Let's say I have an HTML form and there is a group of radio buttons.
A need different script to be run depending on which button is checked.
Let's say I have this:

<input type="radio" name="invtype" value="product">
		<b>PRODUCT<b>
		<p>
		<input type="radio" name="invtype" value="software">
		<b>SOFTWARE<b>
		<p>
		<input type="radio" name="invtype" value="service">
		<b>SERVICE<b>
		<p>
		<input type="radio" name="invtype" value="slogan">
		<b>SLOGAN<b>

How do I do that?
I'm thinking of doing multiple hidden submit buttons, but I don't know how to run PHP scripts off them.
I used to do it through {header}, but when I do that, a parameter that I need to be passed to the next file, somehow doesn't get passed. Or I misunderstand something...

Member Avatar for diafol

Don't do that!!!

You have a choice of client-side scripts or server-side. If you want something to happen before you send the form - use JS (but this isn't foolproof - JS is not secure), or if you want something specific to happen after you've submitted the form, use a php script.

Your HTML is not valid - well not to XHTML spec anyway:

<input type="radio" id="product" name="invtype" value="product" checked="checked" /><label for="product">product</label>
<input type="radio" id="software" name="invtype" value="software">
<label for="software">software</label>
<input type="radio" id="service" name="invtype" value="service">
<label for="service">service</label>
<input type="radio" id="slogan" name="invtype" value="slogan">
<label for="slogan">slogan</label>

Close all tags including empty tags.
Use label instead of <p> - this is semantically correct.
Use CSS or style tags in head section to style labels/inputs etc - don't include this in the HTML, e.g.
Notice that the first input receives a checked="checked" attribute/value. This is to ensure one item in the list of options is checked (you can choose any one of the radios - it doesn't have to be the first one).

input[type="radio"] + label{
  text-transform:uppercase;
  font-weight: bold;
  margin-left:20px;
  display:block;
  height: 20px;
}
input[type="radio"]{
  float:left;
}

BTW: I just wrote the CSS of the top of my head - not tested!

OK, the pHp bit:

Your form will be sent to either the same page/file or another page/file. Either way, you need to check for the form submission at the top of that page/file.

if(isset($_POST['submit_id'])){
//I've assumed your submit button has, value="submit_id" 
   switch($_POST['invtype']){
      case 'product':
         //do something
         break;
      case 'software':
         //do something else
         break;
      case 'service':
         //do something else again
         break;
      case 'slogan':
         //do yet something else
         break;
   }
}

Thank you! You are right - my HTML was sloppy, but the real problem is - the parameter doesn't get passed to the next file.
I do this:

<form action="product.php" method="post">
<input type="text" name="inventorid" value="<?php echo $inventorid; ?>" />
</form>
<?php
echo $inventorid;
switch($_POST['invtype']){
      case 'product':
         {include("product.php");}
         break;
      case 'software':
         //do something else
         break;
etc...

It works just fine.
However when product.php opens, it doesn't seem to have received $inventorid from the previous form.
This is the beginning of product.php:

<?php
	$inventorid=$_POST['inventorid'];
	echo $inventorid;
?>

There is no output from echo...

Member Avatar for diafol

I'm confused - you seem to have a circular reference - although it may just be my slow brain after my science club capers! I can't see where some variables are set. Please post the relevant bits from each page - or is everything from product.php? Are you posting to the same page?

I'm sorry for being confusing.
I am posting from page1.php to product.php page.

Here is my page1.php:

<html>
<?php
// Get the data from the last form
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$homephone = $_POST['homephone'];
$cellphone = $_POST['cellphone'];
$fax = $_POST['fax'];
$email = $_POST['email'];
$website = $_POST['website'];
$street = $_POST['street'];
$city = $_POST['city'];
$zip = $_POST['zip'];
$country = $_POST['country'];
$occupation = $_POST['occupation'];
$age = $_POST['age'];
$invtype = $_POST['invtype'];


$dbhost = 'myhost.com';
$dbuser = 'myuser';
$dbpass = 'mypassword';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die                      ('Error connecting to mysql');

$dbname = 'mydb';
mysql_select_db($dbname);
 
// Insert a new row into the database
$sql = "INSERT INTO inventors (firstname, lastname, homephone, cellphone, fax, email, 
website, street, city, state, zip, country, occupation, age, invtype) 
VALUES ('$firstname', '$lastname', '$homephone', '$cellphone', '$fax',
'$email', '$website', '$street', '$city', '$state', '$zip', '$country', 
'$occupation', '$age', '$invtype')";

mysql_query($sql) or trigger_error(mysql_error(), U_USER_ERROR);
 

$inventorid = mysql_insert_id();
?>
<form action="product.php" method="post">
<input type="text" name="inventorid" value="<?php echo $inventorid; ?>" />
</form>
<?php
switch($_POST['invtype']){
      case 'product':
         {include("product.php");}
         break;
      case 'software':
         //do something else
         break;
      case 'service':
         //do something else again
         break;
      case 'slogan':
         //do yet something else
         break;
   }

?>
</html>

Here is a bit of product.php file that I think would be enough for troubleshooting:

<?php
	
	$inventorid=$_POST['inventorid'];
?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:php="http://php.net/xsl" lang="en"><head xmlns=""><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Product</title>
<link rel="stylesheet" href="mystyle.css" type="text/css" />
<style type="text/css">
</style></head>

<form action="product_submit.php" method="post">
    <input type="text" name="inventorid" value="<?php echo $inventorid; ?>" />
<input type="text" name="invname" />
//etc. - more input elements that will be populated by user (unlike //the "inventorid" that is supposed to be pre-populated...
</form>	
</html>
Member Avatar for diafol

IN addition to name="inventorid", add id="inventorid". I'm not sure if it will work, but I can't see anything wrong with your code - mind you - my head's like a rotten apple at the moment.

In your first page:

<input type="text" name="inventorid" value="<?php echo $inventorid; ?>" />

Just check to see if the php actually outputs anything - look at "view page source" in your browser. However, I'm a little confused as to why you're using a textbox for this - actually, thinking about it - if the php is outputting anything, it'll be seen in the textbox.

I'd use a hidden input field for this so that it can't be changed.

IN addition to name="inventorid", add id="inventorid". I'm not sure if it will work, but I can't see anything wrong with your code - mind you - my head's like a rotten apple at the moment.

In your first page:

<input type="text" name="inventorid" value="<?php echo $inventorid; ?>" />

Just check to see if the php actually outputs anything - look at "view page source" in your browser. However, I'm a little confused as to why you're using a textbox for this - actually, thinking about it - if the php is outputting anything, it'll be seen in the textbox.

I'd use a hidden input field for this so that it can't be changed.

That's exactly what I'm going to do - to make both 'inventorid' textboxes - the one on the first page as well as the other one on the second page - hidden. I just made them visible temporarily for myself - to see the output without opening the source code. :)
The php on the first page outputs the correct number - just what's expected.
The problem is that nothing gets output on the second page.
I just tried using 'id', it didn't help...

Member Avatar for diafol

OK, I've just noticed - you're sending the form to the include file (or trying to). This won't work. You actually have to send the request, usually via submit button to a new page (products.php). An include file will not accept a post variable as the form hasn't been sent.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.