this is my combo box page coding:
<?php
echo "<form method=post align=center>
<div id=myDiv>
<select name=userselect>
<option value=empty></option>
<option value=Confirm> Confirm </option>
<option value=Processing> Processing </option>
<option value=Pending> Pending </option>
<option value=Cancelled> Cancelled </option>
</select>
<button type=button name=combobox value=combobox onclick=loadXMLDoc()>Update</button>
</div>
</form>";
?>
this is my php code (another page):
<?php
if (isset($_POST['combobox']))
{
$userselect = $_POST['userselect'];
echo $userselect;
}
?>
if user select one value in combo box that value should store in db and then display what they are selected. for example if user select CONFIRM option that value stored in db after that it display CONFIRM instead of that combo box form and button. Here i can stored the combo box values in db successfully. thats not a problem and then here i used ajax method to show the msg instead of button place. Now, i want what user select it should display the same value. check these above coding and reply me ur suggestions...
Replace Your code with this:
<?php
//Connect To your Database File..
//After Connecting To Databse
$combo = mysqli_real_escape_string('yourconnectionlink', $_POST['userselect'])
echo "<form method='post' action='youractionfile.php' align='center'>
<div id='myDiv'>
<select name='userselect'>
<option value='empty'></option>
<option value='Confirm'> Confirm </option>
<option value='Processing'> Processing </option>
<option value='Pending'> Pending </option>
<option value='Cancelled'> Cancelled </option>
</select>
<input type='button' name='combobox' value='combobox'>Update</button>
</div>
</form>";
//Insert Data into Database File
if($_POST['userselect'])) {
if(isset($_POST['userselect'])) {
$query = "INSERT INTO tablename (your field names) VALUES ('".$combo."')";
$res = mysqli_query('yourconnectionlink', $query);
echo '1 row inserted';
}
}
?>
finally i got it what i want. without using ajax method i can store the data to db and then combobox, button hide permanently if once we updated the status. this is my final coding...
if ( $a_row['status'] != 'empty' ) {
echo "\t<td>" . $a_row[$status] . "</td>\n";
}
else {
echo "\t<td><form action=statusdb.php method=post>
<select name=update>
<option value=empty></option>
<option value=Confirm>Confirm</option>
<option value=Processing>Processing</option>
<option value=Pending>Pending</option>
<option value=Cancelled>Cancelled</option></select>
<input name=id type=hidden value='".$a_row['slno']."';>
<input type=submit value=Update>
</form>
</td>\n";
}
statusdb coding:
if (isset($_POST['id']))
{
$id = mysql_real_escape_string($_POST['id']);
$update= mysql_real_escape_string($_POST['update']);
$sql = mysql_query("UPDATE guest_details SET status = '$update' WHERE slno = '$id'");
if(!$sql)
{
die("Error" .mysql_error());
}
}
i posted another question (php combobox & button should hide once updated into mysql db and show success message instead of combobox & button place.). i got help from that answer... so, only i posted the correct answer here..
Related
On a web app i'm making, I have an edit page which is obviously where users edit info on already existing products, it uses PHP/ MySQL. My edit page is ALMOST working exactly how I want it too. I have a drop-down populated from the SQL database, and I can edit the product info via the text-boxes. The only problem I have is that I do not know how to edit the products via the ID which changes as the products are selected.. I instead have to manually set a static value in the PHP code, I was just wondering how I could do this? I'm thinking GET could be used but not entirely sure.. I'll post pictures and code below.. thanks.
The ID changes to the corresponding product ID as I select a product from the dropdown
As you can see I have to type in the ID in the php..
so basically i was trying to get it to change the product to edit as i select them from the dropdown..
hope i wasnt too confusing, thanks!
$conn = new mysqli('', '', '', '')
or die ('Cannot connect to db');
$result = $conn->query("select ID, NAME from PRODUCTS");
?><div align="left"><?
print "<h3>EDIT PRODUCT</h3>";
print "<strong>SELECT PRODUCT:</strong>";
print "<br><br>";
print "<form method='GET' id='frm_product'>";
print "<select name='ID' OnChange='$(\"#frm_product\").submit();'>";
while ($row = $result->fetch_assoc())
{
print "<option value='$row[ID]'>$row[NAME]</option>";
}
print "</select>";
print "</form>";
?>
<form method='POST'>
<h3>PRODUCT:</h3>
<input type='textbox' name='product' value='<?php echo $product['product'] ? >'>
<h3>BARCODE:</h3>
<input type='textbox' name='barcode' value='<?php echo $product['barcode']; ?>'>
<h3>TYPE:</h3>
<select name="type">
<option value=""></option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
<br><br>
<input type='submit' value='Save Changes' name=submitform>
</form>
<?
if (isset($_POST['submitform']))
{
$namechange = $_POST[product];
$barcodechange = $_POST[barcode];
$typechange = $_POST[type];
$sql = "UPDATE PRODUCTS SET
NAME='$namechange'
,BARCODE='$barcodechange'
,TYPE='$typechange'
WHERE ID='79'";
if (mysqli_query($conn, $sql)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($conn);
}
mysqli_close($conn);
}
Cant you set:-
$productID = $_GET['ID'];
And then change the 79 on the update script to $productID?
Hopefully I understood the question.
You can do something like this in javascript.
redirect to the product page each time the user selects an option.
<select id="products" onchange="myFunction()">
<option value="Audi">Audi
<option value="BMW">BMW
<option value="Mercedes">Mercedes
<option value="Volvo">Volvo
</select>
<script>
function myFunction() {
var id = document.getElementById("products").value;
window.location.href = "/linkto/product.php?"+id;
}
</script>
If you don't want to reload the page each time you can use jQuery Ajax and JSON
If I get you right you want to edit the entries in the databse which belong to a special ID, right? As you tried with $_GET you must add this to your sql query aswell:
[...]
$typechange = $_POST['type'];
// end of your old code
// start of new code
$id = $_GET['ID'];
// modified sql query
$sql = "UPDATE PRODUCTS SET NAME='$name', BARCODE='',TYPE='' WHERE ID='$id'";
// rest of your old code
[...]
Note: It´s not recommended to use user input without validating. A user can change the values for any existing ID by just manipulating the $_GET parameter in your url. You should validate the users input before processing.
EDIT!!! : LINK = http://i299291.iris.fhict.nl/PHP31/DV3/DV3.php
My problem:
I've made two dropdown boxes with several options. The php code is working and the query gets the right result from the database. But now i want to compare two options.
This is what i've got so far, the problem now is the entire page refreshes when i enter the second value from the other dropdown box.
<html>
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
<form id = "leftDropdown" action= "" method="post">
<select name="objectLinks">
<option value="school">School</option>
<option value="klas">Klas</option>
<option value="geslacht">Geslacht</option>
<option value="lengte">Lengte (CM)</option>
<option value="kg">Gewicht (KG)</option>
<option value="opleiding">Opleiding Ouders</option>
<option value="leeftijdJaar">Leeftijd</option>
<option value="interventie">Deelname interventie?</option>
<option value="pestenVoor">Pestincidenten voor interventie</option>
<option value="pestenNa">Pestincidenten na interventie</option>
<option value="bmi">BMI waarde</option>
<option value="overgewicht">Overgewicht</option>
<option value="allochtonenPerc">Percentage Allochtonen</option>
</select>
<input type="submit" name="sendLinks" value="Go!">
</form>
<form id = "rightDropdown" action= "" method="post">
<select name="objectRechts">
<option value="school">School</option>
<option value="klas">Klas</option>
<option value="geslacht">Geslacht</option>
<option value="lengte">Lengte (CM)</option>
<option value="kg">Gewicht (KG)</option>
<option value="opleiding">Opleiding Ouders</option>
<option value="leeftijdJaar">Leeftijd</option>
<option value="interventie">Deelname interventie?</option>
<option value="pestenVoor">Pestincidenten voor interventie</option>
<option value="pestenNa">Pestincidenten na interventie</option>
<option value="bmi">BMI waarde</option>
<option value="overgewicht">Overgewicht</option>
<option value="allochtonenPerc">Percentage Allochtonen</option>
</select>
<input type="submit" name="sendRechts" value="Vergelijk!">
</form>
<div id = "leftDiv">
<?php
include_once 'dv3ToDB.php'; // connect to database *local or at school's server*
session_start();
if(isset($_POST['sendLinks'])){
$selectedValLinks = $_POST['objectLinks'];
// echo "Jij selecteerde: ".$selectedVal;
echo "<script>console.log('$selectedValLinks');</script>";
// $_SESSION["valLinks"] = $selectedValLinks;
// echo $_SESSION["valLinks"];
$query = "SELECT ($selectedValLinks) FROM pesten ORDER BY ($selectedValLinks) * 1";
$result = $conn->query($query);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "Gevonden data in ".$selectedValLinks.": " . $row["$selectedValLinks"] . "<br>" ;
}
}else{
echo "0 results";
}
}
?>
</div>
<div id = "rightDiv">
<?php
if(isset($_POST['sendRechts'])){
$selectedValRechts = $_POST['objectRechts'];
// echo "Jij selecteerde: ".$selectedVal;
echo "<script>console.log('$selectedValRechts');</script>";
// $_SESSION["valRechts"] = $selectedValRechts;
// echo $_SESSION["valRechts"];
$conn = mysqli_connect($host,$username,$password,$database)
or die("verbinding mislukt:".mysqli_connect_error());
$query = "SELECT ($selectedValRechts) FROM pesten ORDER BY ($selectedValRechts) * 1";
$result = $conn->query($query);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "Gevonden data in ".$selectedValRechts.": " . $row["$selectedValRechts"] . "<br>" ;
}
}else{
echo "0 results";
}
}
?>
</div>
</body>
</html>
i'm running it local now, let me know if you need me to build a database online to help me out.
How can i code it so the PHP output stays in de left DIV and/or the right DIV?
Thank you so much guys and girls! :)
If you need to use only PHP, without any JavaScript (which could allow you to display the result from the database after selecting each option without reloading the page), I'd suggest that, after submitting one form, you add it's result as a hidden input for the other form, so you could read this variable after submitting the 2nd form.
Like this:
<? // Your code for getting the result of the 1st form above returns a variable $res1
// Now insert this into the 2nd form:?>
<input type="hidden" name="stored2" value="<?echo $res1;?>"/>
This way, you can read the variable $_POST["stored2"] after submitting the 2nd form to get what the 1st form returned before.
Do the same for the 1st form to store the results of the 2nd form if it was filled first.
This way, you can compare the results of submitting 2 forms while using only PHP.
EDIT:
You'll need to place all the database requests before the forms to use this method, and just echo the result in the div later.
You want to make a new <iFrame> and then set that <iFrame>'s id as the target for your form.
e.g.:
<form ... target="newframe">
...
</form>
<iFrame id="newframe"></iFrame>
As Lal mentioned you don't need two forms, just one will work.
You need some code that updates the option to selected, if you don't want it to "stick" on page refresh.
Further reading here;
http://www.w3schools.com/tags/att_option_selected.asp
and here
html select option SELECTED
Even try Jquery;
http://forum.jquery.com/topic/how-to-dynamically-select-option-in-dropdown-menu
I have a table in HTML that gets built from a WHILE () { }
inside this table, I have a drop down menu and on select, I have the onchange= command to execute a part of PHP code above on same file. Everything works well, except when I have multiple result in my while because I can't distinguish each form ... how can I do this.
I have a hidden value. but how do I manage this from the if (isset($_POST['status'])) stand point ?
SAMPLE CODE
<?php
if (isset($_POST['status']))
{ }
?>
HTML HERE
<?php
while ($bookingrequest2 = mysql_fetch_array( $bookingrequest ))
{
echo "<tr><td>
<form action=\"".$_SERVER['PHP_SELF']."\" method=\"post\" name=\"reply\">
<select name=\"status\" class=\"form-field\" onchange=\"reply.submit();\" >
<option selected value=\"0\">Make a Selection</option>
<option value=\"Approve\">Approve</option>
<option value=\"1\">Modify</option>
<option value=\"Decline\">Decline</option>
</select>
<input type=\"hidden\" value=\"".$bookingrequest2['booking_id']."\" name=\"booking_id\"/>
</form></td></tr>";
}
?>
Add a hidden input field that identifies the database row. And change reply.submit() to this.form.submit(), because you can't refer to reply as a single element when there are multiple forms with the same name.
while ($bookingrequest2 = mysql_fetch_array( $bookingrequest ))
{
echo "<tr><td>
<form action=\"".$_SERVER['PHP_SELF']."\" method=\"post\" name=\"reply\">
<select name=\"status\" class=\"form-field\" onchange=\"this.form.submit();\" >
<option selected value=\"0\">Make a Selection</option>
<option value=\"Approve\">Approve</option>
<option value=\"1\">Modify</option>
<option value=\"Decline\">Decline</option>
</select>
<input type='hidden' name='id' value='{$bookingrequest2['id']}'>
</form></td></tr>";
}
Then the form processing code can do:
$booking_id = $_POST['id'];
I'm new in PHP.. I need your help..
I have 2 dropdownlist that related:
dropdown 1 : manually insert the value
dropdown 2 : attach value from database (value based on condition that selected in dropdown 1)
Then, both value which are selected will display in textbox at another form.
My problem is:
1) The value in 2nd dropdown can't be display.
2) The value in 1st dropdown can pass to other form but the 2nd can't.
Please kindly guide me.
I don't know how to share my code here.
form1.php
//1st dropdown
<select name="fruit_name" id="fruit_name" style="font-family: Calibri;font-size: 10pt;" onchange="loadXMLDoc(this.value); ">
<option value="0">-- please choose --</option>
<option value="Pineapple">Pineapple</option>
<option value="Apple">Apple</option>
//2nd dropdown
$fruit_name = $_POST['fruit_name'];
#Connect to MySQL
#Connect to database
$result = mysql_query("SELECT colour FROM fruit WHERE fruit_name = '$fruit_name'");
echo "<select name='colour' id='colour' style='font-family: Calibri;font-size: 10pt;'>";
while($row = mysql_fetch_assoc($result))
{
echo "<option value = ''>" . $row['colour'] . "</option>";
}
echo "</select>";
mysql_free_result($result);
//Closes specified connection
?>
form2.php
<?php
//connection
$fruit_name = $_POST['fruit_name'];
$colour = $_POST['colour'];
?>
<label>
<input type="text" name="fruit_name" id="fruit_name" value = "<?php echo $fruit_name;?>" readonly>
</label>
<p>
<label>
<input type="text" name="colour" id="colour" value="<?php echo $colour;?>" readonly>
</label>
</p>
I usually don't do this but since I've some spare time on hand right now, I'm going to give the general approach that you can follow:
Include the following between your <head> tag.
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
Below that, paste this code
<script type="text/javascript">
$(function(){
$('select#fruit_name').change(function(){
var selectedVal = $(this).val(); // get the selected value
$.ajax({ // send ajax request to the php file to process data
type:'post',
url:'php-page-name.php',
data:{'value':selectedVal},
success:function(ret) // display the result from php-page-name.php page
{
$('div#result').html(ret);
}
});
});
});
</script>
Lets move on to your HTML now
<select name="fruit_name" id="fruit_name" style="font-family: Calibri;font-size: 10pt;">
<option value="0">-- please choose --</option>
<option value="Pineapple">Pineapple</option>
<option value="Apple">Apple</option>
</select>
<div id="result">
<select>
<option>Select One</option>
</select>
</div>
php-page-name.php page (Do not forget to create this page and put it in the same folder as form1.php)
<?php
// put the code to connect to your database here
$fruit_name = $_POST['value']; // this will contain the value selected from first dropdown
$result = mysql_query("SELECT colour FROM fruit WHERE fruit_name = '$fruit_name'");
echo "<select name='colour' id='colour' style='font-family: Calibri;font-size: 10pt;'>";
while($row = mysql_fetch_assoc($result))
{
echo "<option value = '".$row['colour']."'>" . $row['colour'] . "</option>";
}
echo "</select>";
mysql_free_result($result);
?>
PS : I'm using the mysql_* functions in this example since I'm assuming you're too. But this is not recommended as they are going to be deprecated soon. You might want to switch to mysqli or PDO
Im trying to implement the search feature in my website.
when the search keyword is entered in the textbox, and the category combo is selected, the form will be Posted and the result will be shown on the same page.
what i want is to keep the selected category of the combo by default in the form after posted
For eg., If i select the category 'Automobiles' in the combo and click search, after form submit, the combo should show the automobiles as default selected option. Please help me. Any help will be appreciated
I assume you get categories from database.
you should try:
<?php
$categories = $rows; //array from database
foreach($rows as $row){
if($row['name'] == $_POST['category']){
$isSelected = ' selected="selected"'; // if the option submited in form is as same as this row we add the selected tag
} else {
$isSelected = ''; // else we remove any tag
}
echo "<option value='".$row['id']."'".$isSelected.">".$row['name']."</option>";
}
?>
Assuming that by "combo" you mean "A regular select element rendering as a drop down menu or list box" and not "A combobox that is a combination of a drop down menu and free text input":
When outputting the <option> elements, check the value against the submitted data in $_POST / $_GET and output selected (in HTML) or selected="selected" (in XHTML) as an attribute of the option element.
Here is the JQuery way I am using.
<select name="name" id="name">
<option value="a">a</option>
<option value="b">b</option>
</select>
<script type="text/javascript">
$("#name").val("<?php echo $_POST['name'];?>");
</script>
But this is only if you have jquery included in your webpage.
Regards
<?php
$example = $_POST["friend"];
?>
<form method="POST">
<select name="friend">
<option value="tom" <?php if (isset($example) && $example=="tom") echo ' selected';?>>Thomas Finnegan</option>
<option value="anna" <?php if (isset($example) && $example=="anna") echo ' selected';?>>Anna Karenina</option>
</select>
<br><br>
<input type="submit">
</form>
This solved my problem.
This Solved my Problem. Thanks for all those answered
<select name="name" id="name">
<option value="a">a</option>
<option value="b">b</option>
</select>
<script type="text/javascript">
document.getElementById('name').value = "<?php echo $_GET['name'];?>";
</script>
$countries_uid = $_POST['countries_uid'];
while($row = mysql_fetch_array($result)){
$uid = $row['uid'];
$country = $row['country_name'];
$isSelected = null;
if(!empty($countries_uid)){
foreach($countries_uid as $country_uid){//cycle through country_uid
if($row['uid'] == $country_uid){
$isSelected = 'selected="selected"'; // if the option submited in form is as same as this row we add the selected
}
}
}else {
$isSelected = ''; // else we remove any tag
}
echo "<option value='".$uid."'".$isSelected.">".$country."</option>";
}
this is my solutions of multiple select dropdown box after modifying Mihai Iorga codes
After trying al this "solves" nothing work. Did some research on w3school before and remember there was explanation of keeping values about radio. But it also works for Select option. See here an example. Just try it out and play with it.
<?php
$example = $_POST["example"];
?>
<form method="post">
<select name="example">
<option <?php if (isset($example) && $example=="a") echo "selected";?>>a</option>
<option <?php if (isset($example) && $example=="b") echo "selected";?>>b</option>
<option <?php if (isset($example) && $example=="c") echo "selected";?>>c</option>
</select>
<input type="submit" name="submit" value="submit" />
</form>
Easy solution:
If select box values fetched from DB then to keep selected value after form submit OR form POST
<select name="country" id="country">
<?php $countries = $wpdb->get_results( 'SELECT * FROM countries' ); ?>
<option value="">
<?php if(isset($_POST['country'])){echo htmlentities($_POST['country']); } else { echo "Select Country *"; }?>
</option>
<?php foreach($countries as $country){ ?>
<option <?php echo ($_POST['country'] == $country->country_name ? 'selected="selected"':''); ?> value="<?php echo $country->country_name; ?>"><?php echo $country->country_name; ?>
</option>
<?php } ?>
</select>