Jump to content

Problems with creating a PHP script with radio buttons and two separate “Submit” buttons where the logic would flow depending on the options chosen...


dennisb

Recommended Posts

I'm trying to create a "Thank you" page where a user lands after he/she has opted into one of our forms. On that page there will be an option for the user to choose a free CD as a gift and will only have to pay for shipping. I would like to give the user an option to either choose a physical CD or a digital MP3. The logic here is that first I provide a user with selection of 4 CDs that I connect to each radio button. Then below that there will be the first Submit button that will say something like "Yes, please mail be a CD." Below the first button there will be a second Submit button that will say something like "Send me the download link instead". Thus if the user clicks on any of the radio buttons and then clicks on the first Submit button, they will be taken to the shopping cart for that CD where they will be prompted to pay the shipping charges for the CD. If the user click on that same radio button, but then clicks on the second Submit button, they will be taken to link to download it. The third link below would just take the user past the page if he chooses to opt out of this offer.

I can't get the script to work correctly for me.

 

Here is what I have so far:

<?PHP

$option1 = 'unchecked';
$option2 = 'unchecked';
$option3 = 'unchecked';
$option4 = 'unchecked';


if (isset($_POST['Submit1'])) 

        {

    $selected_radio = $_POST['cd_choice'];

        if ($selected_radio == 'option1') 
        {
            $option1 = 'checked';
                     ?>
                      <a href="#">Purchase link 1</a>
                     <?php

        }
        else if ($selected_radio == 'option2') 
        {
        $option2 = 'checked';
                     ?>
                      <a href="#">Purchase link 2</a>
                     <?php          
        }
        else if ($selected_radio == 'option3') 
        {
        $option3 = 'checked';
                     ?>
                      <a href="#">Purchase link 3</a>
                     <?php          
        }
        else if ($selected_radio == 'option4') 
        {
        $option4 = 'checked';
                     ?>
                      <a href="#">Purchase link 4</a>
                     <?php          
        }
    }
else
    {
    $selected_radio = $_POST['cd_choice'];

        if ($selected_radio == 'option1') 
        {
         $option1 = 'checked';
                     ?>
                      <a href="#">Download link 1</a>
                     <?php          
        }
        else if ($selected_radio == 'option2') 
        {
         $option2 = 'checked';
                     ?>
                      <a href="#">Download link 2</a>
                     <?php          
        }
        else if ($selected_radio == 'option3') 
        {
         $option3 = 'checked';
                     ?>
                      <a href="#">Download link 3</a>
                     <?php          
        }
        else if ($selected_radio == 'option4') 
        {
         $option4 = 'checked';
                     ?>
                      <a href="#">Download link 4</a>
                     <?php          
        }
    } 
?>

Here's what I have for the HTML portion of it:

<body>

<FORM NAME ="form1" METHOD ="POST" ACTION ="radioButton_test.php">

  <INPUT TYPE = 'Radio' Name ='cd_choice'  value= 'option1' <?PHP print $option1; ?>>
  This is the first CD
  <INPUT TYPE = 'Radio' Name ='cd_choice'  value= 'option2' <?PHP print $option2; ?>>
  This is the second CD
  <INPUT TYPE = 'Radio' Name ='cd_choice'  value= 'option3' <?PHP print $option3; ?>>
  This is the third CD
  <INPUT TYPE = 'Radio' Name ='cd_choice'  value= 'option4' <?PHP print $option4; ?>>
  This is the fourth CD
<P>
<center><INPUT TYPE = "Submit" Name = "Submit1"  VALUE = "Yes, please mail me a CD"></center>
<p><center><INPUT TYPE = "Submit" Name = "Submit2"  VALUE = "Send me download link instead"></center></p>
<div align="center"><a href="#">No thanks. </a></div>
</FORM>

Any help would be greatly appreciated. Thank you!

 

Link to comment
Share on other sites

I tested it as posted and it works for me. What does it do wrong for you?

 

Hi David! It doesn't work for me because if, for example, I click on the first radio button "This is the first CD" and then I click on the first submit button "Yes, please mail me a CD", nothing happens. Nothing happens if I click on the second submit button either. I know that I'm missing some logic somewhere; however, I'm unable to connect the dots and figure out exactly what's missing.

 

Here's an example of what I need it to do. When someone clicks on the first radio button "This is the first CD" and then clicks on the first submit button "Yes, please mail me a CD", the user will be taken to a shopping cart link for that specific CD that user has selected. If the user clicks on that same radio button, but then clicks on the second submit button "Send me download link instead", the user will be taken to a download link for that same MP3. This needs to happed for every selection. This is the reason why two submit buttons were created, so that the user has an option of either getting a physical or digital version of the same CD.

 

Please let me know if this makes sense. Thank you!

Link to comment
Share on other sites

you could try something like this

<?php
    if (isset($_POST['cd_choice'])) {
        $cd = $_POST['cd_choice'];
        
        switch($_POST['Submit1']) {
            case "Yes, please mail me a CD":
                header("location: mailCD.php?cd=$cd");
                exit;
                break;
            case "Send me download link instead":
                header("location: downloadCD.php?cd=$cd");
                exit;
                break;
        }
    }
    else {
        echo "<p>Please select a CD</p>";
    }
?>
<FORM NAME ="form1" METHOD ="POST" ACTION ="">

  <INPUT TYPE = 'Radio' Name ='cd_choice'  value= '1'>
  This is the first CD<br>
  <INPUT TYPE = 'Radio' Name ='cd_choice'  value= '2'>
  This is the second CD<br>
  <INPUT TYPE = 'Radio' Name ='cd_choice'  value= '3'>
  This is the third CD<br>
  <INPUT TYPE = 'Radio' Name ='cd_choice'  value= '4'>
  This is the fourth CD<br>
<P>
<center><INPUT TYPE = "Submit" Name = "Submit1"  VALUE = "Yes, please mail me a CD"></center>
<p><center><INPUT TYPE = "Submit" Name = "Submit1"  VALUE = "Send me download link instead"></center></p>
<div align="center"><a href="#">No thanks. </a></div>
</FORM>
Link to comment
Share on other sites

Thank you so much, Barand! I tried your version of the code. Whenever I click on any of the "Submit" buttons, the page just refreshes.

 

Here's the exact code I'm using.

<?php
    if (isset($_POST['cd_choice'])) {
        $cd = $_POST['cd_choice'];
        
        switch($_POST['Submit1']) {
            case "cd":
                header("location: mailCD.php?cd=$cd");
                exit;
                break;
            case "mp3":
                header("location: downloadCD.php?cd=$cd");
                exit;
                break;
        }
    }
    else {
        echo "<p>Please select a CD</p>";
    }
?>


<FORM NAME ="form1" METHOD ="POST" ACTION ="">

  <INPUT TYPE = 'Radio' Name ='cd_choice'  value= '1'>
  This is the first CD<br>
  <INPUT TYPE = 'Radio' Name ='cd_choice'  value= '2'>
  This is the second CD<br>
  <INPUT TYPE = 'Radio' Name ='cd_choice'  value= '3'>
  This is the third CD<br>
  <INPUT TYPE = 'Radio' Name ='cd_choice'  value= '4'>
  This is the fourth CD<br>
<P>
<center><input type="image" name="Submit1" src="images/mail-me-a-hard-copy-cd.gif" VALUE = "cd"></center>
<p><center><input type="image" name="Submit1" src="images/email-me-download-link.gif" VALUE = "mp3"></center></p>
<div align="center"><a href="#">No thanks. </a></div>
</FORM>

I'm using a custom image for the submit button, which is why the submit code is different from what it was before. That shouldn't matter. Should it?

 

I placed the PHP code right above the form in the body. Is there anything that I'm missing?

 

Thanks!

Link to comment
Share on other sites

I think one problem with the code as posted may be that the header statements are case sensitive:

The first element of the header (i.e. "Location") is case sensitive depending on the browser. In IE7, <?php header("location:http://www.example.com"); ?> does not work as expected whereas <?php header ("Location:http://www.example.com"); ?> does work as expected.

and you use a lower case l in Location.

http://php.net/manual/fr/function.header.php

 

Another issue is the form action is blank.

Edited by davidannis
Link to comment
Share on other sites

Here's the code that I currently have. I changed everything to lowercase, but it's still not working. For the "Read testimonial" script, I have the code and it's working fine, so I didn't include it here.

<?php
    if (isset($_POST['cd_choice'])) 
		{
        	$cd = $_POST['cd_choice'];
        
        switch($_POST['Submit1']) {
            case "cd":
                header("location: mailCD.php?cd=$cd");
                exit;
                break;
            case "mp3":
                header("location: downloadCD.php?cd=$cd");
                exit;
                break;
        }
    }
    else {
        echo "<p>Please select a CD</p>";
    }
?>

<form name ="form1" method ="post" action ="">
<p>
<img src="1.jpg" align="right" /><input type = 'Radio' Name ='cd_choice'  value= '1'><font size="+1"><b>CD 1</b></font> <a href="#" onClick="showStuff('testimonial1'); return false;">Read testimonial</a><br>
<span id="testimonial1" style="display: none;">
<br /><br />“Fascinating! Amazing CD!”<br><br>
John Doe, Someplace USA
</span>
  </p>
<p> </p>

<p>
<img src="2.jpg" align="right" /><input type = 'Radio' Name ='cd_choice'  value= '2'>
  <font size="+1"><b>CD 2</b></font> <a href="#" onClick="showStuff('testimonial2'); return false;">Read testimonial</a><br>
<span id="testimonial2" style="display: none;">
<br /><br />"Highly suggest this. Great cd."<br><br>
Jane Doe
</span>
</p>
<p> </p>

<p>
<img align="right" src="3.jpg" /><input type = 'Radio' Name ='cd_choice'  value= '3'>
  <font size="+1"><b>CD 3</b></font> <a href="#" onClick="showStuff('testimonial4'); return false;">Read testimonial</a><br>
<span id="testimonial4" style="display: none;">
<br /><br />“Remarkable compilation”<br><br>
Mr. Smith
</span>
</p>
<p> </p>
  
<p>
<img align="right" src="4.jpg" /><input type = 'Radio' Name ='cd_choice'  value= '4'>
  <font size="+1"><b>CD 4</b></font> <a href="#" onClick="showStuff('testimonial3'); return false;">Read testimonial</a><br>
<span id="testimonial3" style="display: none;">
<br /><br />"Great cd"<br><br>
Jack Doe
</span>
</p>
<p> </p>

<img align="right" src="5.jpg" /><input type = 'Radio' Name ='cd_choice'  value= '5'>
  <font size="+1"><b>CD 5</b></font></p>
  
<center>
  <p>     </p>
  <p> </p>
 <p>     </p>
  <p>
    
<input type="image" name="Submit1" src="images/mail-me-a-hard-copy-cd.gif" value = "cd"> 
  </p>
</center>
<p><center><input type="image" name="Submit1" src="images/email-me-download-link.gif" value = "mp3"></center></p>
<div align="center"><a href="#">No thanks. </a></div>

</form>
Link to comment
Share on other sites

@davidannis - yes it is so it submits back to itself

 

@dennisb - when you click an input of type image it submits the form but value attributes are ignored. Instead it is posted as the x,y coordinates of the click position of the image. EG

 

array (

    [submit1_x] = x

    [submit1_y] = y

)

 

However, if you name the images as Submit1['cd'] and Submit1['mp3'] then they are posted as

 

array (

    [submit1] = array (

        [mp3] = y

    )

)

 

so you can tell which was clicked from the key value

Link to comment
Share on other sites

@davidannis - yes it is so it submits back to itself

 

@dennisb - when you click an input of type image it submits the form but value attributes are ignored. Instead it is posted as the x,y coordinates of the click position of the image. EG

 

array (

    [submit1_x] = x

    [submit1_y] = y

)

 

However, if you name the images as Submit1['cd'] and Submit1['mp3'] then they are posted as

 

array (

    [submit1] = array (

        [mp3] = y

    )

)

 

so you can tell which was clicked from the key value

 

Hi Barand! I'm a bit confused on the syntax of where I would place this code and what exactly I need to do. Please let me know if I understood you correctly:

 

  1. In the "Submit" tags, the names need to be changed so that it will be displaying as:
    <input type="image" name="Submit1['cd']" src="images/mail-me-a-hard-copy-cd.gif" value = "cd"> 
    

    and

     

    <input type="image" name="Submit1['mp3']" src="images/email-me-download-link.gif" value = "mp3">
    
  2. Then I need to place the following code somewhere.

     

    <script>
    array (
        [Submit1] = array (
        [mp3] = y
        )
    )
    </script>
    

    And this will resolve the issue?

Please let me know if I understood you correctly.

 

Thank you!

Link to comment
Share on other sites

1. Yes - but remove the "value=xx" as they are redundant.

 

2. Those arrays are not code, they are examples of how the form data will be posted for processing.

 

If you out this code

 

echo '<pre>'.print_r($_POST, 1).'</pre>';

in your processing page you will see the data posted from the form so you can then work out how to process it

Link to comment
Share on other sites

1. Yes - but remove the "value=xx" as they are redundant.

 

2. Those arrays are not code, they are examples of how the form data will be posted for processing.

 

If you out this code

echo '<pre>'.print_r($_POST, 1).'</pre>';

in your processing page you will see the data posted from the form so you can then work out how to process it

 

Thanks. I made the changes. My submit looks like this now

<input type="image" name="Submit1['cd']" src="images/mail-me-a-hard-copy-cd.gif">

and

<input type="image" name="Submit1['cd']" src="images/mail-me-a-hard-copy-cd.gif">

Also, I used this code that you gave me below and placed it below the </form> tag

echo '<pre>'.print_r($_POST, 1).'</pre>';

And this is the result that I'm getting.

Array(    [cd_choice] => 5    [Submit1] => Array        (            [\'mp3\'] => 19        ))

The page is still reloading regardless of choice I'm selecting, so it looks like my choices are still not passing.

Link to comment
Share on other sites

I have a version working with

html

<input type="image" name="Submit1[cd]" src="add-icon.gif" value = "cd"> 
  </p>
</center>
<p><center><input type="image" name="Submit1[mp3]" src="add-icon.gif" value = "mp3">

I only changed image names cause it was easier than renaming my existing gif (use your own image name/path). Note the change you want is no quotes around cd and mp3

 

and I changed the switch statement to:

$first_key = key($_POST['Submit1']);
        switch($first_key)

Though you could just

switch(key($_POST['Submit1']))
Edited by davidannis
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.