Jump to content

pull database info depending on form variables


pandaweb

Recommended Posts

ive constructed a form with various drop down lists, radio buttons and check boxes. i would like to pull information from a database, any items pertaining to the option selected, depending on the variables selected in the form.

 

this is the best way i can explain it and was unable to locate anything pertaining to this using search.

 

any help/resources/code examples is greatly appreciated

I don't know a whole lot but I just got something like this working. I had a drop down list that would get the values of my table. You need a loop to get them, an array to store them and another loop to display them.

 

so get the info like this

 

$mysql = new mysqli('localhost', 'root', 'your db pass', 'your database') or die('not working');

$result = $mysql->query("SELECT * FROM your table") or die($mysql->error);

if($result)
{
$name = array();
while($row = $result->fetch_object())
{	
	$name[] = $row->names;

}
foreach($name as $n):

	echo $n . "<br />";
	endforeach;
}

 

so the above code will echo out the results of the name field on name per line. Using the code below, it will populate a drop down list with the same names

<form action="remove.php" method="post">
<select name="dropname">
<?php foreach ($name as $n): ?>
<option value="<?php echo $n; ?>"><?php echo $n ?></option>
<?php endforeach; ?>
</select>
<input type="submit" name="go" value="delete name"  />
</form>

 

to get the values from the form, you set the method to post then you can get them this way

 

on the button for the form, you give it a name.

 

<input type="submit" name="submit" value="submit name" />

 

the name field is the name of the variable for php, then use the post array to get the value

 

$name = $_POST[name];

 

 

for my purposes, I wanted the dropdown list to be populated with the names I submit in the form using the code above, then if I chose a name in the drop down list, it will delete the name from the database using this code

 

$result = mysql_query("DELETE FROM mytable WHERE names = '$_POST[dropname]'");   

 

notice the use of $_post[dropname]

 

dropname is the variable I am using the drop down list. Look above on the select tag, you can see name="dropname"

 

 

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.