I created a drop down list that has been populated by the database and now I'm having trouble retrieving the data. Normally, I would know how to retrieve the value of the drop down list if I had to manually name the data, but in this case, I'm not quite sure how I would name it.
Here is my current code:
<h1>Generate Reports</h1>
<form enctype="multipart/form-data" action="http://localhost/yiiFolder/index.php/create" method="post">
<table>
<tr>
<td><strong>Materials</strong></td>
<?php
mysql_connect('host', 'root', 'password');
mysql_select_db ("db");
$sql = "SELECT material_name FROM materials";
$result = mysql_query($sql);
echo "<td><select name='materials'>";
while ($row = mysql_fetch_array($result))
{
echo "<option value='" . $row['material_name'] . "'>" .
$row['material_name'] . "</option>";
}
echo "</select></td></tr> ";
$sql2 = "SELECT location_name From locations";
$result2 = mysql_query($sql2);
?>
<td><strong>Locations</strong></td>
<?php
echo "<td><select name='locations'>";
while ($row2 = mysql_fetch_array($result2))
{
echo "<option value='" . $row2['location_name'] . "'>" .
$row2['location_name'] . "</option>";
}
echo "</select></td></tr>";
?>
<tr>
<td><button name="submit" type=submit>Generate</button></td>
</tr>
</table>
</form>
<?php
$material = $row['material_name'];
$locations = $row2['location_name'];
$generate = $_POST['submit'];
if(isset($generate))
{
echo $material;
echo $locations;
}
?>
You're trying to capture value before the submit button is hit. Also, as Hanky pointed out you're using the wrong names while referring to select data. You should do this instead
if(isset($_POST['submit'])) // this code will run after the button is clicked
{
$material = $_POST['materials']; // and not material_name
$locations = $_POST['locations']; // and not location_name
echo $material;
echo $locations;
}
PS: You're following a very unsecure way of developing a web application. At the very least you need to switch to PDO and always escape the data.
Related
I have a problem with my page. I was trying to solve it by a lot of tutorials but i don´t know how to make it work. Simply put i have a database of objects. When i select object, page will redirect to another where are shown all informations about the object. But i need to keep the selected option in drop down menu. There is 110 objects so if i select object number 25, informations will show but the drop down menu wont stay on number 25. Can somebody help me with it?
<form action="dbobj2.php" method="post" name="form1">
Zoznam objektov<br>
<?php
include('System/connect.php');
$sql = "SELECT Objekt FROM DBObj";
$result = mysqli_query($db,$sql);
echo "<select name='Objekt'>";
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['Objekt'] . "'>" . $row['Objekt'] . "</option>";
}
echo "</select>";
?>
<input name="btnSubmit" type="submit" value="Vyber">
</form>
<?php
echo $_POST['Objekt'];
echo "<hr>";
$strSQL = "SELECT * FROM DBObj WHERE Objekt = '".$_POST['Objekt']."' ";
$objQuery = mysqli_query($db,$strSQL);
$objResult = mysqli_fetch_array($objQuery);
$imgRes=$objResult['URLobr'];
echo '<img src="http://page.sk.sk/imgs/'.$imgRes.'" alt="obj" height="600" width="800"/>';
echo "<hr>";
echo $objResult['Text'];
?>
First page php
<form action="dbobj2.php" method="post" name="form1">
Zoznam objektov<br>
Vyberte si zvolený objekt z menu a stlačte tlačidlo výber<br>
<?php
include('System/connect.php');
$sql = "SELECT Objekt FROM DBObj";
$result = mysqli_query($db,$sql);
echo "<select name='Objekt'>";
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['Objekt'] . "'>" . $row['Objekt'] . "</option>";
}
echo "</select>";
?>
Check whether the post value matches the object id in the loop.
$selcted = $_POST['Objekt'] == $row['Objekt'] ? ' selected' : '';
If it does $selected is set to " selected" and added to the option.
echo "<option value='" . $row['Objekt'] . "'" . $selected . ">" . $row['Objekt'] . "</option>";
You can send that value $_POST['Objket'] via a variable let us say $selected_value and in the option tag you can write
<option value='" .$row['Objekt']. "' '.if($row['Objket']==$selected_value) echo selected.'>" .$row['Objekt']. "</option>
When you select a option and click on button then it will work otherwise no.
if(isSet($_POST['Objekt']))
{
echo $_POST['Objekt'];
echo "<hr>";
$strSQL = "SELECT * FROM DBObj WHERE Objekt = '".$_POST['Objekt']."' ";
$objQuery = mysqli_query($db,$strSQL);
$objResult = mysqli_fetch_array($objQuery);
$imgRes=$objResult['URLobr'];
echo '<img src="http://page.sk.sk/imgs/'.$imgRes.'" alt="obj" height="600" width="800"/>';
echo "<hr>";
echo $objResult['Text'];
}
I can't pass the selected value to the listing page. When I select an option and click search, a default value passing to listing page. (Default value: last added value)
$sql = mysqli_query($con, "select * from jobregestation order by id");
while ($row = $sql->fetch_assoc()) {
$id = $row['id'];
$name = $row['JobName'];
echo "<option value=" . $id . ">" . $name . "</option>";
}
Thanks in advance.
Are you looking forward something like that ?
<?php
if(isset($_POST['submit'])){ // worked when click on serarch button
echo $_POST['exampleName'];
}
?>
<form action="" method='post'>
<select name="exampleName">
<?php
$sql = mysqli_query($con, "select * from jobregestation order by id");
while ($row = $sql->fetch_assoc()) {
$id = $row['id'];
$name = $row['JobName'];
echo "<option value=" . $id . ">" . $name . "</option>";
}
?>
</select>
<input type="submit" name="submit" value="Search"/>
</form>
<select>
<?php
foreach ($sql->fetch_assoc() as $key=>$value)
{
echo "<option value = " .$value['id'] . ">" .$value['Jobname]."</option>";
}
?>
</select>
This should work.
When I load my page, the value of the variable, $v_UpdateONE, is "Select Version". When I select a version, the value goes blank.
I need to grab the selected value for use in a DB update statement.
Thank you for any assistance. -James
<FORM METHOD="post" ACTION="Update.php" WIDTH="50">
<?php
$avQuery = "SELECT $v_software1 FROM version_master.vermas_availableversions WHERE $v_software1 IS NOT NULL ORDER BY SortCol DESC";
$a_AvailVers = mysql_query($avQuery);
#_Version dropdown box
echo "<select NAME='AvailVersONE' ONCHANGE=submit()>";
echo "<option>Select Version</option>";
while ($row = mysql_fetch_array($a_AvailVers)) {
echo "<option value='" . $row['$v_software1'] . "'>" . $row[$v_software1] . "</option>";
}
echo "</select>";
$v_UpdateONE = $_POST['AvailVersONE'];
echo $v_UpdateONE;
?>
</FORM>
You have an error in
value='" . $row['$v_software1'] . "'
Since $v_software1 is in single quotes, it will be literal $v_software1.
Try removing the quotes -
value='" . $row[$v_software1] . "'
You need to post before you can read $_POST data.
Form File
<FORM METHOD="post" ACTION="Update.php" WIDTH="50">
<?php
$avQuery = "SELECT $v_software1 FROM version_master.vermas_availableversions WHERE $v_software1 IS NOT NULL ORDER BY SortCol DESC";
$a_AvailVers = mysql_query($avQuery);
#_Version dropdown box
echo "<select NAME='AvailVersONE' id='AvailVersONE' ONCHANGE=submit()>";
echo "<option>Select Version</option>";
while ($row = mysql_fetch_array($a_AvailVers)) {
echo "<option value='" . $row['$v_software1'] . "'>" . $row[$v_software1] . "</option>";
}
echo "</select>";
?>
<button type="submit"> <!-- this will draw a submit button -->
</FORM>
then on your Update.php
<?php
$v_UpdateONE = $_POST['AvailVersONE'];
echo $v_UpdateONE;
?>
Sometimes, the ID needs to be filled up (browser dependent)
This is my code for the selection list, i need to modify the code so instead of a drop-down select list, its a bunch of radio buttons (whithout hard coding).
<?php
mysql_connect('hostname', 'username', 'password');
mysql_select_db('database-name');
$sql = "SELECT studentID FROM student ";
$result = mysql_query($sql);
echo "<select name='studentID'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['studentID'] . "'>" . $row['studentName'] . "</option>";
}
echo "</select>";
?>
Any help would be appreciated.
Try following
while ($row = mysql_fetch_array($result)) {
echo '<input type="radio" name="studentID" value="'.$row['studentID'].'"> '.$row['studentName'].'<br>';
}
I would like to apologize if the duplicate of this question exist. i tried to find and could find anything here that could solve my problem..
I am using a form to get the input and update it in the mysql database, and then retrieve the records in the html form, and have defined the code for deleting the records individually through hyperlinks. however i want to do more, i want to use the checkboxes to delete the multiple records.
my code goes like this.
<?php
//include connection string
include('connection.php');
?>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post"/>
Username : <input type="text" name="user"/><br />
Password : <input type="password" name="pass"/><br />
<input type="submit" name="submit" value="Send"/>
</form>
<?php
// query to insert into database
if(isset($_POST['user']) && isset($_POST['pass'])) {
$user = empty($_POST['user']) ? die(mysql_error()) : mysql_escape_string($_POST['user']);
$pass = empty($_POST['pass']) ? die(mysql_error()) : sha1(mysql_escape_string($_POST['pass']));
$query = "INSERT INTO users(name, pass) VALUES ('$user', '$pass')";
$result = mysql_query($query) or die(mysql_error());
}
if(isset($_GET['id'])) {
//query to delete the records
$query = "DELETE FROM users WHERE id = " . intval($_GET['id']);
$result = mysql_query($query);
}
//query to retrieve records
$query = "SELECT * FROM users";
$result = mysql_query($query);
if(mysql_num_rows($result) > 0 ) {
echo "<table cellpadding=10 border=1>";
while ($row = mysql_fetch_row($result)) {
echo "<tr>";
echo "<td>" . $row[0] . "</td>";
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $row[2] . "</td>";
echo "<td>delete";
echo "</tr>";
}
echo "</table>";
}
?>
i would like you to know that i am a newbie to programming world and i am not so sure of how exactly html checkbox work and how do i use it to delete the multiple records. i want to know what extra code do i have to write for it, and i would appreciate a lot if someone explains me that extra code in brief..
thank you..
This is probably a good time for another form:
<?php
// query to insert into database ...
// ... etc...
if(isset($_POST["formDeleteSelected"])) {
//query to delete the records
$query = "DELETE FROM users WHERE id IN (" . implode(", ",$_POST["rowid"]) . ")";
$result = mysql_query($query);
header("Location: mycode.php"); // just so 'refresh' doesn't try to run delete again
exit();
}
?>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
<?php
//query to retrieve records
$query = "SELECT * FROM users";
$result = mysql_query($query);
if(mysql_num_rows($result) > 0 ) {
echo "<table cellpadding=10 border=1>";
while ($row = mysql_fetch_row($result)) {
echo "<tr>";
echo "<td><input type="checkbox" name="rowid[]" value=\"" . $row[0] . "\" /></td>";
echo "<td>" . $row[0] . "</td>";
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $row[2] . "</td>";
echo "</tr>";
}
echo "</table>";
}
?>
<input type="submit" name="formDeleteSelected" text="Delete Selected" />
</form>
Or something like that (I haven't actually tried that code so there may be a typo). Also note that you should make sure to sanitize any form/get inputs for SQL Injection (plenty of information on that in other Stack Overflow questions).
First of all you need a checkbox and the id you want to delete:
<input id="delete" type="checkbox" name="delete" /><label for="delete">Delete user</label>
<input type="hidden" name="user_id" value="12345" />
You can then test if the checkbox has been set and then manually set the GET parameter to reuse your existing code:
if(isset($_POST['delete'])){
$_GET['id'] = $_POST['user_id'];
}
That's not the most elegant solution but a really simple one that should work with your code.
try an SQL query with a list of IDs
... WHERE id=$sentIds[0] OR id=$sentIds[1] OR ...
or use a set operation
... WHERE id IN ($i1,$i2 ... );
You sure have to send ids in the form for this to work, but You know that ;)