PHP update textbox when value of dropdown is changed - php

I filled my dropdown menu with data from the database. I can select here the name of the product. I have a second textbox on my page. Here i want to show the price of the selected product. I tried different ways but I o net get it working.
First of all this is my dropdown menu where the product name is showed:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM form";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<select class='form-control select2' id='product1' name='product1' style='width: 100%;'>";
echo "<option selected disabled hidden value=''></option>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<option onchange='OnChange()' value='" . $row["id"]. "'>" . $row["name"]. "</option>";
}
echo "</select>";
} else {
echo "0 results";
}
$conn->close();
?>
I want to show the price of the selected value in this textbox:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dbsi";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT price FROM form WHERE id='". $product1 ."'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<input type='text' class='form-control' name='price1' id='price1' onkeyup='getValues()' value='" . $row["price1"]. "'>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Using this script i only get "0 results" in price1. It doesnt update when i change the selected value of the dropdown menu. How can I ensure that price1 gets updated when i select another value in the dropdown menu?
Update 1:
I tried to add the the following script
<script>
function OnChange(){
UpdatePoints(<?php echo $price1; ?>);
}
echo "<script type='text/javascript'> window.onchange=load; </script>";
</script>
When I use this script i still get "0 results"
Update 2:
The following is still not working:
//filename: test.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM forms";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<select class='form-control select2' id='product1' name='product1' onChange='getstate(this.value);' style='width: 100%;'>";
echo "<option selected disabled hidden value=''></option>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<option value='" . $row["id"]. "'>" . $row["name"]. "</option>";
}
echo "</select>";
} else {
echo "0 results";
}
$conn->close();
?>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM forms WHERE id='". $product1 ."'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<div id='price-list'>";
echo "<input type='text' class='form-control' name='price1' id='price1' value='" . $row["price"]. "'>";
echo "</div>";
}
} else {
echo "0 results";
}
$conn->close();
?>
<script>
function getprice(val) {
$.ajax({
type: "POST",
url: "test.php",
data:'id='+val,
success: function(data){
$("#price-list").html(data);
}
});
}
</script>
<?php
$product1=$_POST['price1'];
?>

make a ajax call and get the value of using post
<script>
function getprice(val) {
$.ajax({
type: "POST",
url: "index.php",
data:'priceid='+val,
success: function(data){
$("#price-list").html(data);
}
});
}
</script>
<select class='form-control select2' id='product1' name='product1' style='width: 100%;' onChange="getstate(this.value);">
//yourcode
</select>
<div id="price-list">
</div>
index.php
<?php
$product1=$_POST['priceid'];
?>

Related

Getting value from dropdown list

I'm trying to get the selected value from a dropdown list.
The list has name 'select_employee', when I press the button with name 'save' I hope to get the value. I'm using a POST to to get the value.
I get the error 'Undefined index: select_employee'.
<div class="form-group">
<h2>Enter Certified Course Details</h2>
<?php
// start of connect db
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "TrainingDB";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//end of connect db
?>
<form id="form" action="" method="post">
<?php
// Drop down list employee
$sql = "SELECT * FROM employees";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "Select Employee<br>";
echo "<select name='select_employee' id='select_employee'>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<option value='" . $row['id'] . "'>" . $row['fname'] . " " . $row['sname'] . " </option>";
}
echo "</select>";
echo "<br>";
}
?>
</form>
<?php
if(isset($_POST['save'])){
echo "save<br>";
$employee=$_POST['select_employee']; // error here
echo "Selected Employee" . $employee . "<br>";
}
?>
<form Employee="/employee_page.php" method="post">
<button type="submit" class="btn btn-primary" name="save" value="save">Save</button>
</form>
</div>
You are using two different form so make sure to use only one form.

Populate form with array values and use form to access database

So I've been able to add one database field's values (Lname) to an array. I then want to use those array values to populate my form, and then pull the database entry from the specified value into a table. Is there a simpler way to do this? I'm not familiar with Jquery.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "my_database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//place Last Name in array
$sql="SELECT Lname FROM Customers";
$result=mysqli_query($conn,$sql);
$lnames=mysqli_fetch_all($result,MYSQLI_ASSOC);
mysqli_free_result($result);
foreach($lnames as $x) {
$a[] = $x['Lname'];
}
$q = intval($_GET['q']);
$sql="SELECT * FROM Customers WHERE Lname = '".$q."'";
$result = mysqli_query($conn,$sql);
echo "<table>
<tr>
<th>Last Name</th>
<th>Email</th>
<th>City</th>
<th>Balance</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['Lname'] . "</td>";
echo "<td>" . $row['Email'] . "</td>";
echo "<td>" . $row['City'] . "</td>";
echo "<td>" . $row['Balance'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
Here is the suggested script I pulled from a different Overflow forum.
<script>
function showUser(str) {
if (str.length == 0) {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
xmlhttp.open("GET", "gethint.php?q=" + str, true);
xmlhttp.send();
}
}
</script>
And also the HTML code.
<div>
<form>
<select onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">Jordan</option>
<option value="2">Kidd</option>
<option value="3">Hairston</option>
<option value="4">Irving</option>
</select>
</form> <br>
<div id="txtHint">
<b>Person info will be listed here.</b>
</div>
</div>

PHP MySQL UPDATE query not posting value from text box

I have a form with a select list populated from a database. When the query is executed, a success message is returned however the database is not updated.
Here is the form:
<form id="formPrice">
<?php
$conn = new mysqli('localhost', 'something', 'something', 'something')
or die ('Cannot connect to db');
$result = $conn->query("SELECT PlotNumber FROM Developments WHERE Development = 'GREENGRAVES' AND Price = 'BOOKED'");
echo "<select name='plot_update'>";
while ($row = $result->fetch_assoc()) {
echo "<option value=\"Plot\">" . $row['PlotNumber'] . "
</option>";
}
echo "</select>";
?>
<input name="price" type="text" id="price">
<input name="update" type="submit" class="update_price">
</form>
Here is the ajax request:
$('.update_price').click(function() {
var FormData = $('form').serialize();
$.ajax({
type: "POST",
url: '../php/update_price.php',
data : FormData,
success:function(html){
document.getElementById("result_two").innerHTML=html;
}
});
return false;
});
Here is the php
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$price = $_POST['price'];
$plot = $_POST['plot'];
$sql = "UPDATE Developments SET Price = '".$price."' WHERE PlotNumber = '".$plot."' ";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
The page displays "record updated successfully" in the result div, however database entry remains unchanged. Any ideas why? I'm stumped.
The error seemed to be with this line in the front end form:
echo "<option value=\"Plot\">" . $row['PlotNumber'] . "</option>";
When I changed to:
echo "<option>" . $row['PlotNumber'] . "</option>";
it worked!
There is issues with form field under php script:
change your php script to the following:
<form id="formPrice">
<?php
$conn = new mysqli('localhost', 'something', 'something', 'something')
or die ('Cannot connect to db');
$result = $conn->query("SELECT PlotNumber FROM Developments WHERE Development = 'GREENGRAVES' AND Price = 'BOOKED'");
echo "<select name='plot'>";
while ($row = $result->fetch_assoc()) {
echo "<option value=" . $row['PlotNumber'] . ">" . $row['PlotNumber'] . "</option>";
}
echo "</select>";
?>
<input name="price" type="text" id="price">
<input name="update" type="button" class="update_price">
Always make sure - Select option value needs to have the actual value of the record and select field will have the form field name.

collecting info from dropdwon list and send it to e-mail

i got one question. i need to collect the information from the dropdows and send e-mail with that info. How can i do this?I want when they select what they want to generate mail and send it and to write this mail and his date in DB table.
<html>
<body>
<form>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "toner_orders";
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT name FROM printers";
$sql2 = "SELECT pieces FROM toners";
$result = mysqli_query($conn, $sql);
$result2 = mysqli_query($conn, $sql2);
echo "<select>";
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['name'] ."'>" . $row['name'] ." </option>";
}
echo "</select>";
echo "<select>";
while ($row = mysqli_fetch_array($result2)) {
echo "<option value='" . $row['peices'] ."'>" . $row['pieces'] ." </option>";
}
echo "</select>";
?>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "toner_orders";
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT name FROM printers";
$sql2 = "SELECT pieces FROM toners";
$result = mysqli_query($conn, $sql);
$result2 = mysqli_query($conn, $sql2);
echo "<select>";
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['name'] ."'>" . $row['name'] ."</option>";
}
echo "</select>";
echo "<select>";
while ($row = mysqli_fetch_array($result2)) {
echo "<option value='" . $row['peices'] ."'>" . $row['pieces'] ." </option>";
}
echo "</select>";
?>
<br><br>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "toner_orders";
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT name FROM printers";
$sql2 = "SELECT pieces FROM toners";
$result = mysqli_query($conn, $sql);
$result2 = mysqli_query($conn, $sql2);
echo "<select>";
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['name'] ."'>" . $row['name'] ." </option>";
}
echo "</select>";
echo "<select>";
while ($row = mysqli_fetch_array($result2)) {
echo "<option value='" . $row['peices'] ."'>" . $row['pieces'] ." </option>";
}
echo "</select>";
?>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "toner_orders";
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT name FROM printers";
$sql2 = "SELECT pieces FROM toners";
$result = mysqli_query($conn, $sql);
$result2 = mysqli_query($conn, $sql2);
echo "<select>";
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['name'] ."'>" . $row['name'] ."</option>";
}
echo "</select>";
echo "<select>";
while ($row = mysqli_fetch_array($result2)) {
echo "<option value='" . $row['peices'] ."'>" . $row['pieces'] ." </option>";
}
echo "</select>";
?>
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

how to make a checkbox selected automatically?

i want to check the checkbox automatic, i tried with below code but its not checking the checkbox
HTML
<?php
$dbHost = 'localhost'; // usually localhost
$dbUsername = 'xxxx';
$dbPassword = 'xxxxxxxxxxx';
$dbDatabase = 'xxxxxxxxxx';
$db = mysql_connect($dbHost, $dbUsername, $dbPassword) or die ("Unable to connect to Database Server.");
mysql_select_db ($dbDatabase, $db) or die ("Could not select database.");
$variable=$_POST['chk1'];
//print $variable;
$checkbox=implode(',',$variable);
//print $checkbox;
$sql_selectcpnn="SELECT * from clientnetworkpricehistory where id in ($checkbox)";
$querycpnn = mysql_query($sql_selectcpnn);
//print$sql_selectcpnn;
echo "<table style='margin:0px;width:364px'>
<tr>
<th>DateTime</th>
<th>By(employee)</th>
</tr>";
while($row = mysql_fetch_array($querycpnn))
{
$clientid=$row['clientid'];
//print$clientid;
$net=$row['net_id'];
//print$net;
$id=$row['id'];
// print$id;
echo "<tr>";
echo "<td>" .date('d.m.Y H:i', $row['datetime']) . "</td>";
echo "<td>" . $row['user'] . "</td>";
echo"<input type='checkbox' name='chk1[]' value= '$id' checked='checked'/>";
echo "</tr>";
}
echo "</table>";
?>
can anyone tell me what would be my code to show a checkboxs as selected.thanks
Try-
echo "<input type='checkbox' name='chk1[]' checked='checked' value= '$id' />";

Categories