Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New to the CF scene
    Join Date
    Jun 2010
    Location
    Los Angeles, CA
    Posts
    7
    Thanks
    3
    Thanked 1 Time in 1 Post

    Processing a Form using php Question

    Hi - I am a new php coder, and need some advice to help me process a form using php.

    What I have on the form are 4 radio buttons - and based on the user's selection, I need to jump to one of four different .php pages.

    What happens is - it only jumps to the correct php page THE SECOND TIME the "Submit" button is hit. I'd like it to jump to the correct php page the very first time the user hits the button.

    Here are the relevant portions of php code:

    PHP Code:
    if (isset($_POST['send'])) {
    switch (
    $_POST['radio']) {
        case 
    'change_email'
            
    $editFormAction 'change-email_working.php';
            break;
        case 
    'Optout':
            
    $editFormAction 'change-optout_working.php';
            break;
        case 
    'Remove_Completely':
            
    $editFormAction 'change-remove_working.php';
            break;
        case 
    'No_changes':
            
    $editFormAction '';
            break;
        default:
            
    $editFormAction '';
            break;
    }

    And here is what the form code looks like:
    Code:
    <form action="<?php echo $editFormAction; ?>"  method="POST" name="form2" target="_blank" id="form2">
      <input name="radio" type="radio" id="change_email" value="change_email" />
      Change your Current Registered Email Address<br />
      <input name="radio" type="radio" id="Optout" value="Optout" />
      Opt Out from future Emailings<br />
      <input name="radio" type="radio"  id="Remove_Completely" value="Remove_Completely" />
      Remove Yourself Completely from our database<br />
      <input name="radio" type="radio" id="No_changes" value="No_changes" checked="checked" />
      Not make any changes<br /><br />
      <input name="send" type="submit" class="style51" value="Submit" id="send" />
    </form>
    If I echo the $editFormAction value on the top of the new _blank page, it displays the correct/anticipated value ... but it never opens the correct page. It is like I am always one iteration behind (since it will open the correct page if I hit Submit a SECOND time after re-selecting the same button).

    Can anyone tell me what I am doing wrong, or how to fix this?

    Thanks in advance for your wisdom!

  • #2
    New to the CF scene
    Join Date
    Jun 2010
    Location
    Los Angeles, CA
    Posts
    7
    Thanks
    3
    Thanked 1 Time in 1 Post
    I have come up with a solution - and am wondering if this is the best way to handle this.

    When the user hits the Submit button, I jump to a different php file that ONLY performs a redirect to one of four pages, based on which radio button that was selected.

    In other words, I made the form action=choose-mod.php - and within choose-mod.php, I check the value of the $_POST['radio'], and then use a switch statement (same as above) to jump to one of four different php files using the header(sprintf("Location: %s", target_file)); statement.

    The above works - but is there a better way? Any thoughts are definitely appreciated!

  • #3
    New Coder
    Join Date
    Jun 2010
    Posts
    22
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Hi, unfortunately I can't help you because I am a beginner. Anyway can you tell what is the meaning of

    $something=' '

    why is there a blank between the two inverted commas (' ')?

    Again, are (! issetxxx) and (issetxxx !) the same?

  • #4
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    No that is how it should be. You get the user's checked radio and then redirect based on that. No need to resubmit the form. You probably don't need to use sprintf in this case as you have the possible values already defined. Your default case however should contain a value at least otherwise you will get a header with no location defined.

    Quote Originally Posted by blankskull View Post
    Hi, unfortunately I can't help you because I am a beginner. Anyway can you tell what is the meaning of

    $something=' '

    why is there a blank between the two inverted commas (' ')?

    Again, are (! issetxxx) and (issetxxx !) the same?
    There isn't a blank there, its just setting it to an empty string. And no !isset and isset()! are not the same thing. The second one will give you a parse error.
    Last edited by _Aerospace_Eng_; 06-20-2010 at 07:33 PM.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • The Following 2 Users Say Thank You to _Aerospace_Eng_ For This Useful Post:

    blankskull (06-20-2010), gregs (06-20-2010)

  • #5
    120
    120 is offline
    Regular Coder
    Join Date
    Nov 2009
    Location
    UK
    Posts
    105
    Thanks
    6
    Thanked 15 Times in 15 Posts
    It's not working because you are relying on the form action being changed which will require two clicks.

    If you want to do it that way I'd use a redirect, something like this:

    PHP Code:
    if (isset($_POST['send'])) {
    switch (
    $_POST['radio']) {
        case 
    'change_email'
        
    header'Location: change-email_working.php' ) ;
            break;
        case 
    'Optout':
        
    header'Location: change-optout_working.php' ) ;
            break;
        case 
    'Remove_Completely':
        
    header'Location: change-remove_working.php' ) ;
            break;
        case 
    'No_changes':
            
    $editFormAction '';
            break;
        default:
            
    $editFormAction '';
            break;
    }

    Personally I would do my processing in a single page with logic therein, but your system will work. It just requires you to maintain several pages.
    There is always plenty of idol work for the devils hands to do
    Registered Linux User 475075 : Project Honeypot

  • #6
    New to the CF scene
    Join Date
    Jun 2010
    Location
    Los Angeles, CA
    Posts
    7
    Thanks
    3
    Thanked 1 Time in 1 Post
    Quote Originally Posted by blankskull View Post
    Hi, unfortunately I can't help you because I am a beginner. Anyway can you tell what is the meaning of

    $something=' '

    why is there a blank between the two inverted commas (' ')?
    The answer is - I did not put the filename to redirect to in between the two 's. Just an oversight on my part.

    Quote Originally Posted by 120 View Post
    Personally I would do my processing in a single page with logic therein, but your system will work. It just requires you to maintain several pages.
    I tried to do this on a single php page, but there needs to be a bunch of HTML bounced to the user when the page first loads, and so I kept getting the "headers already sent" error.


    Quote Originally Posted by _Aerospace_Eng_ View Post
    ... You probably don't need to use sprintf in this case as you have the possible values already defined. Your default case however should contain a value at least otherwise you will get a header with no location defined.
    I'm struggling with the syntax of how to call this correctly:

    This works: header(sprintf("Location: %s", $editFormAction));
    This does not work: header('Location: %s', $editFormAction);

    Can you give me a pointer to make the second of the above correct?

    Thanks!

  • Users who have thanked gregs for this post:

    blankskull (06-21-2010)

  • #7
    120
    120 is offline
    Regular Coder
    Join Date
    Nov 2009
    Location
    UK
    Posts
    105
    Thanks
    6
    Thanked 15 Times in 15 Posts
    Nothing wrong with being a beginner, so am I ;-)

    If you need to dump HTML output on the first load, go ahead and do it using an 'else' for your 'isset($_POST['send']' set. That is, if it's the first load of the page without the form being submitted the 'else' will run (here using the HEREDOC wrappers to make the concept clearer I hope)

    PHP Code:
    if (isset($_POST['send'])) {
    switch (
    $_POST['radio']) {
        case 
    'change_email'
        
    header'Location: change-email_working.php' ) ;
            break;
        case 
    'Optout':
        
    header'Location: change-optout_working.php' ) ;
            break;
        case 
    'Remove_Completely':
        
    header'Location: change-remove_working.php' ) ;
            break;
        case 
    'No_changes':
            
    $editFormAction '';
            break;
        default:
            
    $editFormAction '';
            break;
    }
    }  else {
    $html = <<<EOH
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Your Title</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta http-equiv="Expires" content="Tue, 01 Jan 1980 1:00:00 GMT">
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <link rel="stylesheet" type="text/css" href="yourcss.css">
    <script type="text/javascript" src="yourjs.js"></script>
    </head>
    <body>
    <p>your html here</p>
    </body>
    </html>
    EOH;
    echo 
    $html;

    Hope that helps or gives you some idea.

    Personally I would break it down further into creating functions for displaying certain elements / scenarios - but don't run before you can walk :-)
    Last edited by 120; 06-20-2010 at 08:46 PM.
    There is always plenty of idol work for the devils hands to do
    Registered Linux User 475075 : Project Honeypot

  • Users who have thanked 120 for this post:

    gregs (06-20-2010)

  • #8
    New to the CF scene
    Join Date
    Jun 2010
    Location
    Los Angeles, CA
    Posts
    7
    Thanks
    3
    Thanked 1 Time in 1 Post

    Thanks!!!

    Thanks guys! I think I am over the hump at this point.

    120 - for a beginner, you are a lot further down the learning curve than me. Thanks for the suggestions and help!

  • #9
    New Coder
    Join Date
    Jun 2010
    Posts
    22
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Hi, Gregs,

    I used the later code. But it shows a warning:

    Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\test\poll.php:3) in C:\xampp\htdocs\test\poll.php on line 15

    The Code:

    <?php


    if (isset($_POST['send'])) {
    switch ($_POST['radio']) {
    case 'change_email':
    header( 'Location: change-email_working.php' ) ;
    break;
    case 'Optout':
    header( 'Location: change-optout_working.php' ) ;
    break;
    case 'Remove_Completely':
    header( 'Location: change-remove_working.php' ) ;
    break;
    case 'No_changes':
    $editFormAction = '';
    break;
    default:
    $editFormAction = '';
    break;
    }
    }

    ?>

    <form action="<?php echo $editFormAction; ?>" method="POST" name="form2" target="_blank" id="form2">
    <input name="radio" type="radio" id="change_email" value="change_email" />
    Change your Current Registered Email Address<br />
    <input name="radio" type="radio" id="Optout" value="Optout" />
    Opt Out from future Emailings<br />
    <input name="radio" type="radio" id="Remove_Completely" value="Remove_Completely" />
    Remove Yourself Completely from our database<br />
    <input name="radio" type="radio" id="No_changes" value="No_changes" checked="checked" />
    Not make any changes<br /><br />
    <input name="send" type="submit" class="style51" value="Submit" id="send" />
    </form>

  • #10
    120
    120 is offline
    Regular Coder
    Join Date
    Nov 2009
    Location
    UK
    Posts
    105
    Thanks
    6
    Thanked 15 Times in 15 Posts
    Make sure you are not sending any html before you get to the chunk (isset($_POST['send'])) ....

    May be better if you post the whole script here (please use the PHP button in the editor to wrap it in tags)
    There is always plenty of idol work for the devils hands to do
    Registered Linux User 475075 : Project Honeypot

  • #11
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    Try this.

    PHP Code:
    <?php
    if(isset($_POST['send']))
    {
      switch(
    $_POST['radio'])
      {
        case 
    'change_email':
          
    $ref 'change-email_working.php';
          break;
        case 
    'Optout':
          
    $ref 'change-optout_working.php';
          break;
        case 
    'Remove_Completely':
          
    $ref 'change-remove_working.php';
          break;
        case 
    'No_changes':
          
    $ref '';
          break;
        default:
          
    $ref '';
          break;
      }

      if(!empty(
    $ref))
      {
        
    header("Location: {$ref}");
      }
    }
    else
    {
    print <<<EOD
    <form action=""  method="post" name="form2" target="_blank" id="form2">
      <input name="radio" type="radio" id="change_email" value="change_email" />
      Change your Current Registered Email Address<br />
      <input name="radio" type="radio" id="Optout" value="Optout" />
      Opt Out from future Emailings<br />
      <input name="radio" type="radio"  id="Remove_Completely" value="Remove_Completely" />
      Remove Yourself Completely from our database<br />
      <input name="radio" type="radio" id="No_changes" value="No_changes" checked="checked" />
      Not make any changes<br /><br />
      <input name="send" type="submit" class="style51" value="Submit" id="send" />
    </form>
    EOD;
    ?>
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #12
    New to the CF scene
    Join Date
    Jun 2010
    Location
    Los Angeles, CA
    Posts
    7
    Thanks
    3
    Thanked 1 Time in 1 Post
    Thanks - I definitely appreciate the excellent input!!! You guys are awesome!!!


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •