I have a radio box and 2 drop down menus which when submitted save to mysql. The radio box is either yes or no and the 2 dropdowns where created in html. It is currently fully working and saves all the data.
What I now wish to do is when a user logs back in, it will show what they have previously selected (if they have).
PHP SCRIPT:
<?php
session_start();
require_once("config.php");
if(!isset($_SESSION['username'])){
header('Location: login.php');
exit;
}else{
$sql = "SELECT attendance1 FROM user WHERE username = '".mysql_real_escape_string($_SESSION['username'])."'";
$res = mysql_query($sql);
$row = mysql_fetch_array($res);
if(($row[0] == "Yes") || ($row[0] == "No")){
header("Location: errorsubmit.html");
exit;
}
}
if(isset($_POST['submit'])){
$sql = "UPDATE user SET attendance1 = '" . mysql_real_escape_string($_POST['attendance1']) . "' WHERE username = '" . mysql_real_escape_string($_SESSION['username']) . "'";
mysql_query($sql) or die("Error in SQL: " . mysql_error());
$sql = "UPDATE user SET colour1= '" . mysql_real_escape_string($_POST['colour1']) . "' WHERE username = '" . mysql_real_escape_string($_SESSION['username']) . "'";
mysql_query($sql) or die("Error in SQL: " . mysql_error());
$sql = "UPDATE user SET shade1= '" . mysql_real_escape_string($_POST['shade1']) . "' WHERE username = '" . mysql_real_escape_string($_SESSION['username']) . "'";
mysql_query($sql) or die("Error in SQL: " . mysql_error());
header("Location: thanks.html", true, 303);
}
?>
FORM:
<form>
<input name="attendance1" type="radio" id="Yes" value="Yes" checked="checked"/>Yes
<br />
<input name="attendance1" type="radio" id="No" value="No" />No
</h3></td>
<td>
<select name="colour1" id="colour1" >
<option selected="selected">Please Select</option>
<option>Red</option>
<option>White</option>
<option>Green</option>
</select>
</td>
<td><h3>
<select name="shade1" id="shade1" >
<option selected="selected">Please Select</option>
<option>light</option>
<option>heavy</option>
</select>
<td> </td>
<td><label>
<input type="submit" name="submit" id="button" value="Submit" />
</label></td>
</tr>
</table>
Try with below:
you need to fetch values from database and match them with select box values to show them selected.
<select name="shade1" id="shade1" >
<option>Please Select</option>
<option value="light" <?php if($val=='light') echo 'selected'; ?>>light</option>
<option value="heavy"<?php if($val=='heavy') echo 'selected'; ?>>heavy</option>
</select>
Here $val is variable having value retrieved from database.
while adding you should have :
<option value="light" >light</option>
<option value="heavy">heavy</option>
You just have to check if the value in the database has the value of the option field, and if so you echo a "selected='true'" to your option tag. Like
<option <?php if($row["column_name"] == "light") echo "selected=\"true\""; ?>>light</option>
Related
I want to show data from a Oracle database into a listbox.
, but I don't no how to do that.
Now I'm using a textbox and that works good.
This is my HTML code
<form name="form1" method="get" action="Get_opdracht.php"
Opdrachtnummer: <br /> <input id="Password1" type="number" name="nummer1" required="required"/>
<input type="submit" name="submit1" value="Zoeken" />
<hr />
</form>
PHP code (get_opdracht.php)
// database connect
$conn = oci_connect('username', 'password', 'connect');
// variable textbox
$username = $_GET['nummer1'];
// SELECT query
$array = oci_parse($conn, "SELECT * FROM OPD_VW, MDW_VW WHERE OPD_OPDRACHTNUMMER = '$username'");
$query = oci_execute($array);
//show data on page
while (($row = oci_fetch_array($array, OCI_BOTH)) != false) {
echo "<h1>Opdrachtnummer: " . $row['OPD_OPDRACHTNUMMER'] . "</h1><p> <b>Status: </b>" . $row['OPD_STATUS'] . "<p><b>Registratiedatum: </b>" . $row['OPD_REGISTRATIEDATUM'] . "<p><b>Einddatum: </b>" . $row['OPD_EINDDATUM'] . "<p><b>BTW tarief: </b>". $row['OPD_BTW_TARIEF'] . "<p><b>Totale contractsom: €</b>" . $row['OPD_TOTALE_CONTRACTSOM'] . "<p><b>Percentage gerealiseerd: </b>" . $row['OPD_PERCENTAGE_GEREALISEERD'] . "%";
oci_free_statement($array);
oci_close($conn);
To create a listbox you need to use the select multiple as shown below.
<select name="myselect" multiple="multiple">
<option value="value">OPTION</option>
<option value="value">OPTION</option>
</select>
I have select (combo boxes) in PHP and after one was selected i fill the second one with data from the database.
the question is how to display my selection from the first combo box after the page reload.
I have the following code:
<select name="category" id="category" maxlength="30" onchange="this.form.submit();">
<option value =""></option>
<?php
require_once("config.php");
// Connect to server
$con = mysql_connect($ServerAddress,$ServerUser,$ServerPassword);
//choose DB
mysql_select_db($DbName, $con);
$sql = "SELECT * FROM `category` ORDER BY `name`";
$res = mysql_query($sql);
while ($row1 = mysql_fetch_array($res)){
echo '<option value ="'.$row1['name'].'">'.$row1['name'].'</option>';
}
?>
</select>
</span>
<label>race:</label>
<span>
<select name="race" id="race" maxlength="30" >
<option value =""></option>
<?php
if (isset($_POST['category'])) {
$var = $_POST['category'];
// Connect to server
$con = mysql_connect($ServerAddress,$ServerUser,$ServerPassword);
//choose DB
mysql_select_db($DbName, $con);
$sql = "SELECT * FROM `race` WHERE `category`='" . sqlSecure($var) . "'ORDER BY `race`";
$res = mysql_query($sql);
while ($row1 = mysql_fetch_array($res)){
echo '<option value ="'.$row1['race'].'">'.$row1['race'].'</option>';
}
}
?>
</select>
Method how to get values are defined in form tag in attribute method
In your case you need to get echo $_POST['category'] and compare it with name of option
Your code are not perfect. Look follows: :)
<?php
require_once("config.php");
// Connect to server
$con = mysql_connect($ServerAddress, $ServerUser, $ServerPassword);
//choose DB
mysql_select_db($DbName, $con);
?>
<select name="category" id="category" maxlength="30" onchange="this.form.submit();">
<option value=""></option>
<?php
$val = $_POST['category']?:'';
$sql = "SELECT * FROM `category` ORDER BY `name`";
$res = mysql_query($sql);
while ($row1 = mysql_fetch_array($res)) {
$selected = ($val == $row1['name'] ? 'selected="selected"' : '');
echo '<option value ="' . $row1['name'] . '" '. $selected .'>' . $row1['name'] . '</option>';
}
?>
</select>
</span>
<label>race:</label>
<span>
<select name="race" id="race" maxlength="30">
<option value=""></option>
<?php
if (isset($_POST['category'])) {
$var = $_POST['category'];
$sql = "SELECT * FROM `race` WHERE `category`='" . sqlSecure($var) . "'ORDER BY `race`";
$res = mysql_query($sql);
while ($row1 = mysql_fetch_array($res)) {
echo '<option value ="' . $row1['race'] . '">' . $row1['race'] . '</option>';
}
}
?>
</select>
I have donation page which when the user clicks donate it posts the data to a php file named test.php I am trying this out my first trying to echo the first name and last name but this is not working ultimately I want this php page to run a MySQL query to update the total_Donation row within a database, here is my main php page first.
Database code which sits at top of file
<?php
$con = mysql_connect("localhost","root","null");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("snr", $con);
$names_sql = "SELECT first_Name, last_Name FROM donate WHERE user_ID > 0";
$names_query = mysql_query($names_sql)or die(mysql_error());
$rsNames= mysql_fetch_assoc($names_query);
if(isset($_POST['donation']) && $_POST['donation'] != '')
{
$donation = mysql_real_escape_string($_GET['donation']);
$fname = mysql_real_escape_string($_GET['first_Name']);
$lname = mysql_real_escape_string($_GET['last_Name']);
$donate_sql = "UPDATE `donate` SET donate_Total = donate_Total + '{$donation}' WHERE first_Name = '{$fname}' AND last_Name = '{$lname}'";
}
mysql_close($con);
?>
Here is my form section of html
form method ="post" action="test.php">
<table>
<tr><td><label>Runner:</label></td>
<td>
<select>
<?php do{?>
<option> <?php echo $rsNames['first_Name'];?> <?php echo $rsNames['last_Name'];?></option>
<?php } while ( $rsNames= mysql_fetch_assoc($names_query))?>
</select>
</td>
</tr>
<tr><td><label>Donation £</label></td><td><input type="text" maxlength="9" value="0.00" name="donation"/></td></tr>
<tr><td><input id="submit" type="submit" value="DONATE"/></td></tr>
</table>
</form>
the option gets all the first names and last names fine when the user hits donate I want it to run the $donation_sql but all i get are errors saying unidentified index, I'm even trying the below in the test.php to simply just echo the first_Name this is giving the same error.
<?php
echo $_POST['first_Name'];
?>
Can someone please help me with this, thanks.
index.php
<?php
$con = mysql_connect("localhost","root","null");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("snr", $con);
$names_sql = "SELECT first_Name, last_Name FROM donate WHERE user_ID > 0";
$names_query = mysql_query($names_sql)or die(mysql_error());
?>
<form method ="post" action="test.php">
<table>
<tr><td><label>Runner:</label></td>
<td>
<select name="name">
<?php
while($list = mysql_fetch_array($names_query))
{
?>
<option value="<?php echo $list['first_Name'] . ' ' . $list['last_Name']; ?>">
<?php echo $list['first_Name'] . ' ' . $list['last_Name']; ?>
</option>
<?php
}
?>
</select>
</td>
</tr>
<tr><td><label>Donation £</label></td><td><input type="text" maxlength="9" value="0.00" name="donation" /></td></tr>
<tr><td><input id="submit" type="submit" name="send" value="DONATE"/></td></tr>
</table>
</form>
test.php
<?php
$con = mysql_connect("localhost","root","null");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("snr", $con);
if(isset($_POST['donation']) && $_POST['donation'] != '')
{
$names = explode(' ',$_POST['name']);
$first_name= $names[0];
$last_name= $names[1];
$donation = mysql_real_escape_string($_POST['donation']);
$fname = mysql_real_escape_string($first_name);
$lname = mysql_real_escape_string($last_name);
$donate_sql = "UPDATE `donate` SET donate_Total = donate_Total + '" .$donation. "' WHERE first_Name = '" .$fname. "' AND last_Name = '" .$lname. "'";
echo 'DEBUG (remove after OK): <br>' .$donate_sql. '<br>';
$res = mysql_query($donate_sql);
echo 'Thanks ' .$first_name. ' ' .$last_name. '<br>';
}
mysql_close($con);
?>
That´s it!
\make sure you set name for select and you have valua attr in option tag
<select name="first_Name">
<otpion value="<?php echo $rsNames['first_Name'];?>"><?php echo $rsNames['first_Name'];?>
<?php echo $rsNames['last_Name'];?>
</option>
</select>
YOu need to give a name attribute to the select:
<select name="first_Name">
<?php while ( $rsNames= mysql_fetch_assoc($names_query)):?>
<option value="<?php echo htmlspecialchars($rsNames['first_Name']).' '.htmlspecialchars($rsNames['last_Name']);?>"> [option displayed to the user here]</option>
<?php endwhile;?>
</select>
And of course use the $_POST array, not the $_GET, since you're using the POST method.
This question already exists:
Closed 11 years ago.
Possible Duplicate:
Populating a dynamic drop down menu from a MySQL database
my form successfully updates mysql database but when the user logs back in I want to assign database value as selected value in a drop down box to what the user has selected before which does not work.
PHP:
<?php
session_start();
require_once("config.php");
if(!isset($_SESSION['username'])){
header('Location: login.php'); /
}
}
if(isset($_POST['submit'])){
$sql = "UPDATE user SET attendance1 = '" . mysql_real_escape_string($_POST['attendance1']) . "' WHERE username = '" . mysql_real_escape_string($_SESSION['username']) . "'";
mysql_query($sql) or die("Error in SQL: " . mysql_error());
$sql = "UPDATE user SET food1 = '" . mysql_real_escape_string($_POST['food1']) . "' WHERE username = '" . mysql_real_escape_string($_SESSION['username']) . "'";
mysql_query($sql) or die("Error in SQL: " . mysql_error());
$sql = "UPDATE user SET drink1 = '" . mysql_real_escape_string($_POST['drink1']) . "' WHERE username = '" . mysql_real_escape_string($_SESSION['username']) . "'";
mysql_query($sql) or die("Error in SQL: " . mysql_error());
header("Location: thanks.html", true, 303);
}
$row2 = "SELECT * FROM user WHERE username = '" . mysql_real_escape_string($_SESSION['username']) . "'";
$result = mysql_query($row2) or die("Error in SQL: " . mysql_error());
$row3 = mysql_fetch_array($result);
echo $row3['shade1'];
?>
FORM:
<form>
<input name="attendance1" type="radio" id="Yes" value="Yes" checked="checked"/>Yes
<br />
<input name="attendance1" type="radio" id="No" value="No" />No
</h3></td>
<td>
<select name="colour1" id="colour1" >
<option selected="selected">Please Select</option>
<option>Red</option>
<option>White</option>
<option>Green</option>
</select>
</td>
<td><h3>
<select name="shade1" id="shade1" >
<option selected="selected">Please Select</option>
<option value="Light" <?php if($row2['shade1']=="Light") { echo "selected"; }?>>Light</option>
<option value="Heavy" <?php if($row2['shade1']=="Heavy") { echo "selected"; }?>>Heavy</option>
</select>
<td> </td>
<td><label>
<input type="submit" name="submit" id="button" value="Submit" />
</label></td>
</tr>
</table>
</form>
You can use JQuery:
1) Use the $.ajax call in $(document).ready() to get the values you want
2) Set them like this:
Say your <select></select> element has an id of "options", set it's currently selected value like this:
$("#options").val(optionToBeSelected);
where optionToBeSelected is the value attribute of the <option></option> element you want to become selected.
This code should work for you.
If anyone sees a major/minor flaw in my answer, please let me know!
I want to know the error in this code
The following code retrieves the names of the members of the database query in the
dropdownlist
But how do I know who you selected.... I want to send messages only to the members that selected form dropdown list
<?php
include ("connect.php");
$name = $_POST['sector_list'];
echo $name ;
?>
<form method="POST" action="" >
<input type="hidden" name="sector" value="sector_list">
<select name="sector_list" class="inputstandard">
<option size ="40" value="default">send to </option>
<?php
$result = mysql_query('select * from members ')
or die (mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo '<option size ="40" value=" '. $row['MemberID'] . '" name="' . $row['MemberName']. '">' . $row['MemberName']. '</option>';
}
?>
</select>
</form>
I hope somebody can help me
This should do the trick.
<?php
$member_id = intval($_POST['sector_list']);
if($member_id == 0) {
// Default choice was selected
}
else {
$res = mysql_query("SELECT * FROM members WHERE MemberID = $member_id LIMIT 1");
if(mysql_num_rows($res) == 0) {
// Not a valid member
}
else {
// The member is in the database
}
}
?>
<form method="post" action="">
<input type="hidden" name="sector" value="sector_list">
<select name="sector_list" class="inputstandard">
<option value="0">send to</option>
<?php
$result = mysql_query('SELECT * from members') or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo '<option value="' . $row['MemberID'] . '">' . $row['MemberName']. '</option>';
}
?>
</select>
</form>
To get an input to change when you select someone try this:
<select onchange="document.getElementById('text-input').value = this.value;">
<!-- Options here -->
</select>
<input type="text" id="text-input">