Jump to content

[SOLVED] Smart PHP'er needed to look at this...


drukpa

Recommended Posts

Hi Guys,

I need someone much smarter than me to tell me what I'm doing wrong here.

 

I have 3 radio buttons, in the same form, with a submit button.

I want my php to check which radio button is selected and based on this, redirect to another url.

 

My HTML:

<form name="form1" method="POST" action="../checkbox_radio.php">

  <label>

    <input type="radio" name="bp" id="bp" value="1">

    Option One</label>

  <label>

    <input type="radio" name="bp" id="bp" value="2">

    Option Two</label>

  <label>

    <input type="radio" name="bp" id="bp" value="3">

    Option Three</label>

               

                <input type="image" src="../images/Add-To-Cart.gif" value="Submit" alt="Submit">

 

  </form>

 

my php file "checkbox_radio.php"

<?PHP

$URL1="http://www.mydomain.com/page1.html";

$URL2="http://www.mydomain.com/page2.html";

$URL3="http://www.mydomain.com/page3.html";

 

$selected_radio = '0';

 

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

{

$selected_radio = $_POST['bp'];

 

if($selected_radio == '1')

{

//$option_one = 'checked';

header("Location: $URL1");

//exit;

}

elseif ($selected_radio == '2')

{

//$option_two = 'checked';

header("Location: $URL2");

//exit;

}

elseif ($selected_radio == '3')

{

//$option_three = 'checked';

header("Location: $URL3");

//exit;

}

}

?>

 

Thank you!

 

Drukpa

Hi NGreenWood6 & Samoth,

 

a - Apologies for not posting the code in the correct tag.

b - NGreenWood6 - The redirecting isn't happening. I get a blank page.

When I change the _POST to _GET i notice that the string also has an &x= and &y= values. I can only guess this is why the if statement check isn't working. The x & y values change all the time - I dont know where this is coming from to start with, otherwise I would've deleted the object if I could.

 

Samoht - I tried the Switch statement - but i get the same result - the php file loads, appearing to load a page, then goes blank...

 

Any ideas?

 

Thanks guys...

Just change it to how you want but that works as it stands.

 



  <?php
  
   if(empty($_GET['action']))  { 
  
  ?> 
  <form action="?action=check" name="myform" method="POST" />
  
  URL ONE: <input type="radio" name="url" id="url" value="1" /> 
   URL TWO: <input type="radio" name="url" id="url" value="2" /> 
    URL THREE: <input type="radio" name="url" id="url" value="3" /> <br />
  <input type="submit" value="submit" /> 
  </form>
  <?php
       }
  ?>

  <?php
   if($_GET['action'] == "check" )  {

   $url = (int)$_POST['url'];

   
   if($url == 0 ) { echo " Error "; }
   elseif($url == 1 ) { echo " URL 1 "; }
   elseif($url == 2 ) { echo " URL 2 "; }
   elseif($url == 3 ) { echo " URL 3 "; }
       }
  ?>

You don't have to change your whole code like burnside tells you. It looks to me like you did not name the submit button change this:

 

input type="image" src="../images/Add-To-Cart.gif" value="Submit" alt="Submit">

 

to this:

 

input type="image" src="../images/Add-To-Cart.gif" value="Submit" alt="Submit" name="Submit">

 

 

All I did there was add the name tag. and oh yeah the method for the form does not have to be capitalized.

 

<form name="form1" method="post" action="../checkbox_radio.php">

You don't have to change your whole code like burnside tells you. It looks to me like you did not name the submit button change this:

 

input type="image" src="../images/Add-To-Cart.gif" value="Submit" alt="Submit">

 

to this:

 

input type="image" src="../images/Add-To-Cart.gif" value="Submit" alt="Submit" name="Submit">

 

All I did there was add the name tag.

 

Bah how could i of missed that GRRRR!!!! well done :P

You don't have to change your whole code like burnside tells you. It looks to me like you did not name the submit button change this:

 

input type="image" src="../images/Add-To-Cart.gif" value="Submit" alt="Submit">

 

to this:

 

input type="image" src="../images/Add-To-Cart.gif" value="Submit" alt="Submit" name="Submit">

 

All I did there was add the name tag.

 

Bah how could i of missed that GRRRR!!!! well done :P

 

 

.......BURN!!!!!.........

 

=)

;D  :o  ::)

 

Thank you guys...That's tremendous!

 

Who would've thought! Luckily i don't earn my living programming!  :D

 

I really do appreciate the input...and the 'read between the lines' name your buttons something other than 'Submit'...

Lesson learnt.

 

Happy holidays to everyone!

 

Respectfully,

 

Drukpa

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.