PHP if() Statement Solution, please.

Discussion in 'Web Design and Development' started by JelvisChan, Nov 14, 2009.

  1. JelvisChan macrumors member

    Joined:
    Jan 9, 2009
    #1
    Hello everyone,

    I am trying to create a form handling php script that, if a certain radio button is checked, you will be redirected to another location depending on which radio button was submitted.

    This is sort of what I have, just changed text:

    HTML CODE:
    HTML:
    <form action="redirect.php" method="post">
    	
    	<input type="radio" name="item" value="books"
    >Barnes & Noble
    
    <input type="radio" name="item" value="clothes"
    >American Eagle
    	
    	
        <input type="submit" name="ok" value="OK" />
        <input type="submit" name="cancel" value="Forget This Transaction" />
    </form>

    And then the PHP CODE that handles the form:

    redirect.php
    PHP:
    <?php

        $radio_selected 
    $_POST['item'];
        
        if (
    $radio_selected == "books") {
            
            
    header("Location: http://www.barnesandnoble.com");
        }
        
        else {
            
                    
    header("Location: http://www.ae.com");
            
        }




    ?>
    The solution would be greatly appreciated.

    Thanks.
     
  2. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #2
  3. JelvisChan thread starter macrumors member

    Joined:
    Jan 9, 2009
    #3
    My problem is that I tried testing it and it doesn't work. It only sends me to the last link, even if I check the first radio button.

    The site you posted doesn't help me.

    My question:

    How do you use PHP to redirect visitors to different URLs depending on which radio button you checked?

    Thanks
     
  4. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #4
    The code you posted works fine for me, that's why I asked about errors. It redirects as you would expect. Do you have access to the PHP error log file? I would look there for clues. If you don't know where it is try adding this to the top of your PHP code.

    PHP:
    error_reporting(E_ALL);
    ini_set('display_errors''On'); // on for development, off otherwise
    ini_set('log_errors''On');
    ini_set('error_log''path/phperr.log');
    Be sure to change the error log path on that last line. It needs an absolute path too. These lines turn on logging and display errors to the screen so you don't need to check the log files.
     
  5. jaikob macrumors 6502

    jaikob

    Joined:
    Jul 1, 2008
    Location:
    Freeland, MI
    #5
    Try this to debug with:

    PHP:
    ini_set('display_errors''On');
    if(isset(
    $_POST['item'])) {

    $radio_selected $_POST['item'];
    if(
    $radio_selected == "books") {
    header("Location: http://www.barnesandnoble.com");
    } elseif (
    $radio_selected == "clothes") {
    header("Location: http://www.ae.com");

    } else {
    echo 
    "Error: The radio fields did not have a value of books or clothes. It had: $radio_selected";
    } else {
    die(
    "POST request not specified!");
    }
    I didn't test it but I am confident it works :)
    By the way it's good practice to check if the post request was sent.
     
  6. JelvisChan thread starter macrumors member

    Joined:
    Jan 9, 2009
    #6
    No, none of your solutions worked.

    I put in the path to the php error file and tested the script, but it only went to my 2nd URL even when I ticked the first radio button.

    For the php script you provided, It only gave me a blank screen when I submitted the form.

    Anyone have any last minute scripts that I could try or something?

    Thanks again!
     
  7. ab2650 macrumors 6502a

    Joined:
    Jun 21, 2007
    #7
    How about just trying to print the value of $_POST['item'] and/or $_POST?

    PHP:
    <?php

        
    print($_POST['item']);
        
    print_r($_POST);

    ?>
     
  8. JelvisChan thread starter macrumors member

    Joined:
    Jan 9, 2009
    #8
    The error message reads:

    Undefined variable: radio_selected
     
  9. vendettabass macrumors 6502a

    vendettabass

    Joined:
    Jul 8, 2006
    Location:
    Seoul, South Korea
    #9
    this worked for me:

    PHP

    PHP:
    <?php

    if(filter_has_var(INPUT_POST'ok')){
        
    $selected $_POST['item'];
        
        switch(
    $selected){
            case 
    "books" :
                
    header("Location: http://www.barnesandnoble.com");
                break;
            case 
    "clothes" :
                
    header("Location: http://www.ae.com"); 
                break;
        
        }
    }

    ?>
     
  10. JelvisChan thread starter macrumors member

    Joined:
    Jan 9, 2009
    #10
    booksArray ( [item] => books [submit] => Submit Query )
     
  11. JelvisChan thread starter macrumors member

    Joined:
    Jan 9, 2009
    #11
    Result:

    booksArray ( [item] => books [submit] => Submit Query )
     
  12. JelvisChan thread starter macrumors member

    Joined:
    Jan 9, 2009
    #12


    BLANK SCREEN
     
  13. JelvisChan thread starter macrumors member

    Joined:
    Jan 9, 2009
    #13
    Everyone I figured out what I did wrong (sort of wrong):

    Instead of using the if..else statement, I used both if...if

    PHP:
    <?php

    if ($selected_radio == "books") {
    header("Location: www.url.com");
    }

    if (
    $selected_radio == "clothes") {
    header("Location: www.otherurl.com");
    }  
    Thanks for all the help -- much appreciated!
     

Share This Page