fill form after option select - php

I have a product list that filled automatically from the DB, I want to get the data corresponding to the selected product and fill the form.
<form name='login' action='<?php echo ($_SERVER['PHP_SELF']); ?>' method='post' enctype="multipart/form-data">
<?php
$sql = "SELECT product_name FROM products ORDER BY product_name";
$result = $mysqli->query($sql);
if ($result->num_rows > 0) {
echo "<select>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<option>" . $row["product_name"]."</option>";
}
echo "</select>";
} else {
echo "0 results";
}
$mysqli->close();
?>
<input type="text" name="product_code" placeholder="Product Code" />
<input type="text" name="product_name" placeholder="Product Name" />
<input type="text" name="product_price" placeholder="Price" />
<input type="file" name="fileToUpload" accept="image/*">
<textarea rows="8" name="product_desc_de" placeholder="Deutsch description"></textarea>
<textarea rows="8" name="product_desc_en" placeholder="English description"></textarea>
<textarea rows="8" name="product_desc_es" placeholder="Spanich description"></textarea>
<button type="submit" class="btn btn-default">Submit</button>
</form>
I know it should be somehow with AJAX

You have not shown any AJAX code so I assume that was just a mistake.
You do not NEED Ajax to make a form work.
So first give your <select> tag a name
echo '<select name="prods">';
And then your <option> tags a value
echo '<option value="' . $row["product_name"] . '">' . $row["product_name"] . '</option>';
Now when you look at the $_POST array you will have a $_POST['prods']
Personally I would add the id field to the SELECT
$sql = "SELECT id, product_name FROM products ORDER BY product_name";
And use the id as the value like this
echo '<option value="' . $row['id'] . '">' . $row['product_name'] . '</option>';

Related

How to post a different value from that shown in a select form input

On my webpage i have the below form. I am using some PHP to display the name of users in the select input. My question is this... how do I save down the id column (that I am selecting in the SQL) to the database instead of the name columns that I am displaying? All help greatly appreciated.
<form action='insertmodule.php' method='POST'>
<span class="form-group">
<input type="text" placeholder="Name" class="form-control" name="name" required/>
</span>
<br>
<div class="form-group">
<label for="exampleSelect1">Select the tutor for the module</label>
<select class="form-control" name="tutor">
<?php
include('../connections/conn.php');
$sql = mysqli_query($conn, "SELECT id, first_name, last_name FROM cater_users WHERE role = 2");
while ($row = $sql->fetch_assoc()){
echo "<option>" . $row['first_name'] . " ". $row['last_name'] . "</option>";
}
?>
</select>
</div>
<button type="submit" class="btn btn-primary" name='sign'>Create</button>
</form>
try this
echo sprintf('<option value="%s">%s %s</option>', $row['id'], $row['first_name'], $row['last_name']);

adding post to database by php form and category

I want to post the value selected from the category table to software table by a php form but when I post the form it is only posting the id instead of the value I selected.
<form name="post" id="form" action="" method="post" enctype="multipart/form-data">
<select name="category" id="category">
<option value="<?php echo $category">
<?php
$categoryqu="SELECT * FROM categories";
$results=mysql_query($categoryqu);
while($row=mysql_fetch_assoc($results)){
$id=$row['cid'];
$name=$row['name'];
echo"<option value='".$id."'>".$name."</option>";
}
?>
</option>
</select>
<input placeholder="Title" name="title" value="<?php echo $title; ?>" type="text">
<input placeholder="Title" name="title" value="<?php echo $link; ?>" type="text">
<input placeholder="Title" name="title" value="<?php echo $description; ?>" type="text">
</form>
Category table is
Category Table Image Link
and it is posting only id instead of category names.
Here is the table posting the data from dropdown
when I post the form it is posting all is well but only the value of selected category is posting the value "id" but I want it post the selected value to the database where and what is the error in this code.
The error you describe with "but when i post the form it posting the id only instead of the value i selected" is the wanted behavior of submitting a HTML select.
First of all you should make sure, that you really need the $name value when saving the data into your database. You should be able to make the same relations with the $id as well.
If you really need it, you can either use the $name as the value attribute instead. If you do this the $id will not be saved.
echo"<option value='" . $name . "'>" . $name . "</option>";
Or you could create a concatenation of both, that you later explode.
echo"<option value='" . $id . '|' . $name. "'>" . $name . "</option>";
Then later when analysing the POST result:
list($id, $name) = explode('|', $selectedValue, 2);
Try This
<form name="post" id="form" action="" method="post" enctype="multipart/form-data">
<select name="category" id="category">
<?php
$categoryqu="SELECT * FROM categories";
$results=mysql_query($categoryqu);
while($row=mysql_fetch_assoc($results)){
$id=$row['cid'];
$name=$row['name'];?>
<option value="<?php echo $id;?>"><?php echo $name; ?></option>
}?>
</select>
<input placeholder="Title" name="title" value="<?php echo $title; ?>" type="text">
<input placeholder="Title" name="title" value="<?php echo $link; ?>" type="text">
<input placeholder="Title" name="title" value="<?php echo $description; ?>" type="text">

populating form with mysql data

Hopefully someone can help with my problem, I have a database using MySQL and I have a form that posts to the database, this all works fine.
I have created a customer look up form using HTML and PHP that echos a table and then the table is filled with the MySQL data, again this all works OK.
My problem is that I would like to click the table row or text in that row which ever and open it in a HTML form and include all the data from the row in the MySQL database.
<div id="content">
<h3>Customer Lookup</h3>
<?php
$con=mysqli_connect("localhost","root","","repairsdb");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM customers");
echo "<table border='1'>
<tr>
<th>First Name</th>
<th>Surname</th>
<th>Address</th>
<th>Postcode</th>
<th>Landline</th>
<th>Mobile</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['surname'] . "</td>";
echo "<td>" . $row['addressl1'] . "</td>";
echo "<td>" . $row['postcode'] . "</td>";
echo "<td>" . $row['landline'] . "</td>";
echo "<td>" . $row['mobile'] . "</td>";
echo "<td>" . $row['businessname'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
<FORM>
</FORM>
</div>
New Code
<h3>Customer Lookup</h3>
<?php
$con=mysqli_connect("localhost","root","","repairsdb");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM customers");
echo "<table border='1'>
<tr>
<th>First Name</th>
<th>Surname</th>
<th>Address</th>
<th>Postcode</th>
<th>Landline</th>
<th>Mobile</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td><a href='/repairdb/customer_form.php?customerid=" . $row['customerid'] . "'>" . $row['firstname'] . "</td>";
echo "<td>" . $row['surname'] . "</td>";
echo "<td>" . $row['addressl1'] . "</td>";
echo "<td>" . $row['postcode'] . "</td>";
echo "<td>" . $row['landline'] . "</td>";
echo "<td>" . $row['mobile'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
customer_form.php
<?php
$con=mysqli_connect("localhost","root","","repairsdb");
if( isset( $_GET['customerid'] ) ) {
$customerid = $_GET['customerid'];
}
$result = mysqli_query($con,"SELECT * FROM customers where customerid = " . $customerid . " Limit 1");
?>
<form method="post">
<input type="text" name="firstname" value="$result['firstname']" />
<input type="text" name="surname" value="$result['surname']" />
<input type="text" name="addressl1" value="$result['addressl1']" />
<input type="text" name="postcode" value="$result['postcode']" />
<input type="text" name="landline" value="$result['landline']" />
<input type="text" name="mobile" value="$result['mobile']" />
<input type="text" name="businessname" value="$result['businessname']" />
</form>
The easiest way to do this would be to open the form in a new page. So to do this we would start by adding a link to your table.
I'm going to assume that in your database you have an unique ID for each customer - we will use this in the link:
echo "<tr>";
echo "<td><a href='/customer_form.php?customer_id=" . $row['ID'] . "'>" . $row['firstname'] . "</a></td>";
echo "<td>" . $row['surname'] . "</td>";
echo "<td>" . $row['addressl1'] . "</td>";
echo "<td>" . $row['postcode'] . "</td>";
echo "<td>" . $row['landline'] . "</td>";
echo "<td>" . $row['mobile'] . "</td>";
echo "<td>" . $row['businessname'] . "</td>";
echo "</tr>";
As you can see I have named the new page customer_form.php. And we will pass the ID to this page via the URL, like so.
www.example.com/customer_form.php?customer_id=3
In your new customer_form.php page you will first need to retrieve the ID from the URL.
if( isset( $_GET['customer_id'] ) ) {
$customer_id = $_GET['customer_id'];
}
Then we will modify this block of code a little more to retrieve the customer from the database, using the ID, and then we can add this data to a new form.
if( isset( $_GET['customer_id'] ) ) {
$customer_id = $_GET['customer_id'];
$result = mysqli_query($con,"SELECT * FROM customers where ID = " . $customer_id . " Limit 1");
echo '
<form method="post">
<input type="text" name="firstname" value="'. $result['firstname'] .'" />
<input type="text" name="surname" value="'. $result['surname'] .'" />
<input type="text" name="addressl1" value="'. $result['addressl1'] .'" />
<input type="text" name="postcode" value="'. $result['postcode'] .'" />
<input type="text" name="landline" value="'. $result['landline'] .'" />
<input type="text" name="mobile" value="'. $result['mobile'] .'" />
<input type="text" name="businessname" value="'. $result['businessname'] .'" />
</form>
';
}
You could do it other ways, in a pop up window, or even on the same page using JavaScript. But this is the least complicated.
I should note that the SQL query I wrote would be vulnerable to SQL injection, since it takes a value from the address bar and injects it directly into the query. But it's a little outside of the scope of the question, so I suggest you look it up if interested.
sounds like this would work...
use clicks on link (selects something in)
then - jqueryAJAX POSTs FORM
then - php processPOST
then - mysql getDataFrom DB
then - php process MYSQL results && echo $jsoResults
then - jqueryAJAX process results open new Form with inputs populated...
I managed to sort the errors, I have posted the code for anyone having the same problem. I have answered this question myself only for the reason so I can post the final code, but the credit has to go to #user1100149 and also a chap on YouTube.
Link to youtube video editing mysql with PHP
<?php
// Connects to Our Database
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("repairsdb") or die(mysql_error());
if(!isset ($_POST['submit'])) {
$q = "SELECT * FROM customers WHERE customerid = $_GET[customerid]";
$result = mysql_query($q);
$person = mysql_fetch_array($result)or die (mysql_error());
}
?>
<form name="submit_customer" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<div id="frcol3">
<fieldset>
<label id="label" for="webaddress">Business</label>
<input name="businessname" type="text" class="inputBox" id="" value="<?php echo $person['businessname']; ?>" />
<label id="label" for="firstname">First Name</label>
<input class="inputBox" type="text" name="firstname" id="" value="<?php echo $person['firstname']; ?>" />
<label id="label" for="Surname">Surname</label>
<input name="surname" type="text" class="inputBox" id="" value="<?php echo $person['surname']; ?>" />
<label id="label" for="landline">Telephone No.</label>
<input name="landline" type="text" class="inputBox" id="" value="<?php echo $person['landline']; ?>" />
<label id="label" for="mobile">Mobile No.</label>
<input name="mobile" type="text" class="inputBox" id="" value="<?php echo $person['mobile']; ?>" />
<label id="label" for="email">Email</label>
<input name="email" type="text" class="inputBox" id="" value="<?php echo $person['email']; ?>" />
</fieldset>
</div>
<div id="frcol2">
<fieldset>
<label id="label" for="webaddress">Web Address</label>
<input name="webaddress" type="text" class="inputBox" id="" value="<?php echo $person['webaddress']; ?>" />
<label id="label" for="addressl1">Address Line 1</label>
<input name="addressl1" type="text" class="inputBox" id="" value="<?php echo $person['addressl1']; ?>" />
<label id="label" for="addressl2">Address Line 2</label>
<input name="addressl2" type="text" class="inputBox" id="" value="<?php echo $person['addressl2']; ?>" />
<label id="label" for="town">Town</label>
<input name="town" type="text" class="inputBox" id="" value="<?php echo $person['town']; ?>" />
<label id="label" for="county">County</label>
<input name="county" type="text" id="" class="inputBox" value="<?php echo $person['county']; ?>" />
<label id="label" for="postcode">Postcode</label>
<input name="postcode" type="text" id="" class="inputBox" value="<?php echo $person['postcode']; ?>" />
</fieldset>
</div>
<div id="frcol1">
<textarea name="notes" id="notes" placeholder="<?php echo $person['notes']; ?>" cols="45" rows="5"></textarea>
<br />
<input type="hidden" name="customerid" value="<?php echo $_GET['customerid']; ?>" />
<input type="submit" name="submit" id="" class=" button" value="Submit" />
</div>
</form>

Editing mysql data with drop downs

I have a data entry page where a few drop downs exist. The selected item in the drop down stores in my mysql database with no issue.
I've made a 2nd page to edit individual records. I can display the data from the drop down in a text box with no problem. However, I want to be able to edit the result with the same drop down choices.
I can add the drop down menu on my edit.php page with all the correct options, but the stored value from the database does not appear. Instead I get the first choice by default and not the stored value.
<?php
$position_sql = "SELECT id, position FROM ref_positions ORDER BY position ASC";
$position_result = mysql_query($position_sql);
echo "<select name='position'>";
while ($row = mysql_fetch_array($position_result)) {
echo "<option value='" . $row['id'] . "'>" . $row['position'] . "</option>";
}
echo "</select>";
?>
I'm using POST and GET to get the correct record ID.
My text boxes, which work fine are as follows:
Department:<input type="text" name="department" size="20" value="<?php echo "$row[department]"; ?>">
I'm assuming I have to build some sort of if statement to display the stored value?
not sure if this helps, but this is my how I grab the ID for the record I want to edit:
<?php
$id= ($_GET["id"]);
$sql = "SELECT * FROM people
WHERE id='$id' LIMIT 1";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
?>
Entire Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Form Edit Data</title>
</head>
<body>
<?php
$BASE_PATH = 'C:\xampp\htdocs\OGS';
include_once($BASE_PATH . "\includes\layouts\header.php");
?>
<div id="main">
<div id="subnavigation">
<?php include_once($BASE_PATH . "\mods\main_menu\index.html");?>
</div>
<div id="page"
<br><br>
<table border=1>
<tr>
<td align=center>Update Employee Information</td>
</tr>
<tr>
<td>
<table>
<?php
mysql_connect('localhost', 'root', '');
mysql_select_db('ogs');
?>
<?php
$id= ($_GET["id"]);
$sql = "SELECT * FROM people
WHERE id='$id' LIMIT 1";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
?>
<form method="post" action="edit_data.php">
<input type="hidden" name="id" value="<?php echo "$row[id]"; ?>">
<fieldset>
<legend><b>Name</b></legend>
First Name:<input type="text" name="first_name" size="20" value="<?php echo "$row[first_name]"; ?>">
Last Name:<input type="text" name="last_name" size="40" value="<?php echo "$row[last_name]"; ?>">
</fieldset>
<br><br>
<fieldset>
<legend><b>Contact Information</b></legend>
Town:<input type="text" name="town" size="20" value="<?php echo "$row[town]"; ?>">
Address:<input type="text" name="address" size="40" value="<?php echo "$row[address]"; ?>">
Province:<input type="text" name="province" size="20" value="<?php echo "$row[province]"; ?>">
Postal Code:<input type="text" name="postal_code" size="40" value="<?php echo "$row[postal_code]"; ?>">
<br><br>
Home Phone:<input type="text" name="home_phone" size="20" value="<?php echo "$row[home_phone]"; ?>">
Cell Phone:<input type="text" name="cell_phone" size="40" value="<?php echo "$row[cell_phone]"; ?>">
</fieldset>
<br><br>
<fieldset>
<legend><b>Emergency Contact</b></legend>
Emergency Contact Name:<input type="text" name="first_name" size="20" value="<?php echo "$row[first_name]"; ?>">
Emergency Contact Number:<input type="text" name="last_name" size="40" value="<?php echo "$row[last_name]"; ?>">
</fieldset>
<br><br>
<fieldset>
<legend><b>Work Information</b></legend>
Role:<input type="text" name="role" size="20" value="<?php echo "$row[role]"; ?>">
Employer:<input type="text" name="company_works_for" size="40" value="<?php echo "$row[company_works_for]"; ?>">
<br><br>
Department:<input type="text" name="department" size="20" value="<?php echo "$row[department]"; ?>">
Position:
<?php
$position_sql = "SELECT id, position FROM ref_positions ORDER BY position ASC";
$position_result = mysql_query($position_sql);
echo "<select name='position'>";
// You should use PHP to get the existing value here, I have made it up here as 14
$existing_id = '$row[id]';
while ($row = mysql_fetch_array($position_result))
{
// Check if the existing id is the same as the current id we are displaying
// If it is, set the selected attribute
if($existing_id == $row['id'])
echo "<option selected='selected' value='" . $row['id'] . "'>" . $row['position'] . "</option>";
else
echo "<option value='" . $row['id'] . "'>" . $row['position'] . "</option>";
}
echo "</select>";
?>
<br><br>
Is Supervisor?:
<input type="radio" name="is_supervisor" value="<?php echo "$row[is_supervisor]"; ?>"> Yes
<input type="radio" name="is_supervisor" value="<?php echo "$row[is_supervisor]"; ?>"> No
<br><br>
Is Active?:
<input type="radio" name="active_employee" value="<?php echo "$row[active_employee]"; ?>"> Yes
<input type="radio" name="active_employee" value="<?php echo "$row[active_employee]"; ?>"> No
<br><br>
Start Date:<input type="text" name="start_date" size="40" value="<?php echo "$row[start_date]"; ?>">
</fieldset>
<input type="submit"
name="submit value" value="Update">
</form>
</div>
</div>
</body>
</html>
The record for Nicholas Furlong shows him as a Health & Safety Coordinator.
http://prntscr.com/3o34g2
But when I click edit, and go to my edit page, it has him listed as controller. (This is the first option in the column.)
http://prntscr.com/3o36qu
Its not entirely clear what you are asking. But I will try
<?php
$position_sql = "SELECT id, position FROM ref_positions ORDER BY position ASC";
$position_result = mysql_query($position_sql);
echo "<select name='position'>";
// You should use PHP to get the existing value here, I have made it up here as 14
$existing_id = 14;
while ($row = mysql_fetch_array($position_result))
{
// Check if the existing id is the same as the current id we are displaying
// If it is, set the selected attribute
if($existing_id == $row['id'])
echo "<option selected='selected' value='" . $row['id'] . "'>" . $row['position'] . "</option>";
else
echo "<option value='" . $row['id'] . "'>" . $row['position'] . "</option>";
}
echo "</select>";
?>

href #div_name?id="" PHP

Is it possible to attach the data to the url like this?
<?php
$query = mysql_query(......) or die(mysql_error());
while($row = mysql_fetch_assoc($query)){
echo "<a href='#foo?id=" . $row['Id'] . "'>" . $row['Name'] . "</a>";
}
?>
<div id="foo">
<form action="process.php" method="post">
<input type="hidden" name="id" value="<?php echo $_GET['id'] ?>"/>
<input type="text" name="message" value=""/>
<input type="submit" name="sendMessage" value="Send Message"/>
</form>
</div>
I want to pass the $row['Id'] to the div so that it can be put in the value of the hidden input type. Thank you for your answers and suggestions!
Try changing your code like this.
<?php
$query = mysql_query(......) or die(mysql_error());
while($row = mysql_fetch_assoc($query)){
echo "<a href='#foo?id=" . $row['Id'] . "'>" . $row['Name'] . "</a>";
$id = $row['Id'] ;
}
?>
<div id="foo">
<form action="process.php" method="post">
<input type="hidden" name="id" value="<?php echo $id ; ?>"/>
<input type="text" name="message" value=""/>
<input type="submit" name="sendMessage" value="Send Message"/>
</form>
</div>
Cheers!
Prasad.
If you're trying to take the row number that is clicked on and then make that the value for the hidden input, You probably want to use some javascript/jQuery. Its not even that difficult. Take a look at an example I made here
<?php echo "<div id=".$_GET['id'].">"?>....html....</div>
When you loop this, I don't know how you're applying it but if it's with a lot of other stuff, you might want to consider building an array and then outputting to a template format.

Categories