Question

I am stuck using radio buttons to display the staffNames with a value of staffID, so that if the user selects a name and submits, details of purchases are shown (linked to another file called task7.php). Thanks for the help i edited my code, I can see the names now just need to link it im getting a black table and error Notice: Undefined index: staffID in I:\twa\twa291\practicals\prac2\task7.php on line 16 HERE IS MY CODE:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-
strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Prac 2 Task 9</title>
</head>

<body>

<?php
$conn = mysql_connect("localhost", "twa291", "......");
mysql_select_db("factory291", $conn)
or die ('Database not found ' . mysql_error() );



?>

<form id= "f1" method="get" action="task7.php">

<?php
$sql = "SELECT staffID, staffName FROM staff";
$result = mysql_query($sql, $conn)
or die ('Problem with query' . mysql_error());

while ($row = mysql_fetch_array($result)){ 

$id=$row["staffID"];
$name=$row["staffName"];
  echo "<input type="radio" name='$name' value='$id'/>"; 

}
?>  

<input type="submit" value="Submit" >
</form>

<?php 
mysql_close($conn); ?>


</body>
</html>
Était-ce utile?

La solution

Your input code formatting is wrong:

  echo "<input type="radio" value='$id'</input>"; 

should be...

  echo "<input type='radio' value='$id' name='staff'> $name<br />"; 

You need to either make all of the quotation symbols within your double quotes single quotes OR escape the double quotes inside the echo with \" as such:

  echo "<input type=\"radio\" value=\"$id\" name=\"staff\"> $name<br />"; 

I also added a name attribute because for radio buttons to work properly they all need to share the same name.

Autres conseils

This line

echo "<input type="radio" value='$id'</input>"; 

should be

echo "<input type='radio' value='$id'/>"; 

Quotes mismatch in input tag.

echo "<input type="radio" value='$id'</input>"; 

Also, the radio button should have a unique name.

Do this, instead:

echo "<input type='radio' name='staff' value='$id'</input>"; 

first you change

echo "<input type="radio" value='$id'</input>"; 

to

   echo "<input type='radio' value='$id'>"; 

pls note:here you doesnt set class name for radio button

Valid example for radio button is

 echo "<input type='radio' name='staff' value='$id'>$name";
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top