as part of my project I have two files:
modify_brand.php (php form)
update_brand.php (script to update the DB)
In File 1. the brands are listed in a select field and displayed in a separated field that allows to modify the brand's name.
The file 2. contains the query that update the DB.
The problem consists of passing the ID of the selected brand to the UPDATE query stored in update_brand.php in order to identify the record to update.
May be I am approaching the matter in a wrong way, but if not do you know how to pass to the query the ID?
modify_brand.php
<form role="form" action='../php/update_brand.php' method='post'>
<label>BRANDS LIST</label>
<select name='brands-list'>
<?php
while ($listabrand=mysqli_fetch_array($brands)){
echo '<option value="'.$listabrand['1'].'">'.$listabrand['0'].' - '.$listabrand['1'].'</option>';
}?>
</select>
<label>BRAND'S NAME TO MODIFY</label>
<input type="text" name='brand-name'>
<button type="submit" name='modify-btn' class="btn btn-default">Modifica</button>
</form>
.....
<script>
$('select[name="brands-list"]').change(function(){
var selectedBrand = $(this).val();
$('input[name="brand-name"]').val(selectedBrand);
});
</script>
update_brand.php
<?php
require '../sys/conn.php';
$mod_brand=$_POST['brand-name']
if (isset($_POST['modify-btn'])){
$brand=mysqli_real_escape_string($conn, $mod_brand] );
if ($mod_brand !=''){
$update = mysqli_query($conn,"
UPDATE mg_terms SET name= $brand
WHERE term_id=......... // THIS IS WHERE THE ID OF SELECTED BRAND SHOULD BE PLACED
");
header('Location: ../pages/success.html');}
else{header('Location: ../pages/error.html');}}
mysqli_close($conn);
?>
Hope I was enoughly clear.
So what you want to do is to set the select options value to the id, So when you submit your form, the selected id is submitted to the php. But you're gonna have to change your Javascript a bit. It should set the input's value to the options text, not the options value:
<form role="form" action='../php/update_brand.php' method='post'>
<label>BRANDS LIST</label>
<select name='brands-list'>
<?php
while ($listabrand=mysqli_fetch_array($brands)){
echo '<option value="'.$listabrand['0'].'">'.$listabrand['0'].' - '.$listabrand['1'].'</option>';
}?>
</select>
<label>BRAND'S NAME TO MODIFY</label>
<input type="text" name='brand-name'>
<button type="submit" name='modify-btn' class="btn btn-default">Modifica</button>
</form>
.....
<script>
$('select[name="brands-list"]').change(function(){
$('input[name="brand-name"]').val($('select[name="brands-list"] option:selected').text().split(' - ')[1]);
});
</script>
And on the php side:
<?php
require '../sys/conn.php';
$mod_brand=$_POST['brand-name'];
$mod_id=$_POST['brands-list'];
if (isset($_POST['modify-btn'])){
$brand=mysqli_real_escape_string($conn, $mod_brand );
$brand_id=intval($mod_id );
if ($mod_brand !=''){
$update = mysqli_query($conn,"
UPDATE mg_terms SET name= '$brand'
WHERE term_id=$brand_id
");
header('Location: ../pages/success.html');}
else{header('Location: ../pages/error.html');}}
mysqli_close($conn);
?>
Related
I have a code which has a form that inputs surface area. db_connect.php connects the database. I am trying to populate a drop down list with a condition that all values that have surface area greater than the value typed into the text field will be displayed in the text field. But when I try to run the code, i'm getting all the values. How can I solve this? Thank you in advance!
<html>
<head>
<title>hi</title>
</head>
<body>
<form>
<p> surface area : <input name = "sa" type = "text"> </p>
<br>
</form>
<select name="areas">
<?php
$sa = $_POST['sa'];
include "db_connect.php";
$displayArea = "SELECT area FROM details where area > '".$sa."'" ;
$sql = mysqli_query($link, $displayArea);
echo "<option> Select </option>";
while ($row = mysqli_fetch_assoc($sql))
{
echo "<option value=\"areas\">" . $row['area'] . "</option>";
}
?>
</select>
</body>
</html>
first you need a submit button into the form.
<input type="submit" value="Submit">
Then if you are using POST you have to specify it as a Form method:
<form method="post">
Then add:
$sa = $_POST['sa'];
echo("[".$sa."]");
to see if "sa" is populated.
If you add a value and click on "Submit" you will see the result.
I have a form wherein I have a drop down which is being populated from the database also I have an input box right beneath it. I want to fetch the value of both the fields via Ajax using jQuery and insert them into the database.
Problem: Value of the text field is getting inserted successfully but the value of drop down is not getting inserted.
I know I will have to fetch the value of the drop down separately and then add it to the data in ajax but I am not able to figure how to do the same.
NOTE: COULD THE DOWN VOTERS BE KIND ENOUGH TO TELL ME WHY I WAS DOWNVOTED SO THAT I COULD IMPROVE UPON THINGS.
sub_category_add.php
<form method="post">
<select id="cat_sub_cat">
<?php
$data=mysqli_query($con,"SELECT * FROM category");
while($row=mysqli_fetch_array($data))
{
echo "<option value=".$row['cid'].">".$row['category']."</option>";
}
?>
</select><br><br>
<h3>Add Sub Categories</h3><br>
<input type="text" name="sub_cat"><br><br>
<input type="submit" name="submit" value="Insert" id="sub_cat_btn">
</form>
Ajax File:
$(document).on('click','#sub_cat_btn', function(event){
event.preventDefault();
$.ajax({
url:"sub_cat_add_back.php",
method:"post",
data:$('form').serialize(),
dataType:"html",
success:function(strMsg){
$("#cat_sub_msg").html(strMsg);
}
})
sub_cat_add_back.php
<?php
include "../includes/config.php";
$name=$_POST['sub_cat'];
$cid=$_POST['cat_sub_cat'];
$data=mysqli_query($con,"INSERT INTO subcategory (name,cid) VALUES ('$name','$cid')");
if($data=="true")
{
echo "Successfully Inserted";
}
else
{
echo "Error";
}
?>
Your problem is that the select tag does not have the Name attribute
<select id="cat_sub_cat" name="cat_sub_cat">
If you want to get values on click instead of form serialize. add id in input
<input type="text" name="sub_cat" id="custom">
$("#cat_sub_cat option:selected");
$("#custom").val();
and if you want to do that with form serialize then add name in your select
<select id="cat_sub_cat" name="your_select_name">
I am using a form having select dropdown. I want to pass the value obtained from the selected option as a $_GET request in form action field but any ways to access it outside the foreach loop. Here is the code sample that I have written
<form id="dynamicForm" action="client-detail-dynamic.php?id=<?php echo $_GET['id']; ?>&r_id=<?php **PASS THE DROPDOWN VALUE ID HERE** ?>" method="post">
<select class="form-control" id="dynamicfy" name="dynamicfy">
<?php
$j = 0;
foreach($payment_data as $pd):
?>
<option value="<?php echo $payment_data[$j]->r_id; ?>"><?php echo $payment_data[$j]->fy; ?></option>
<?php $j++; endforeach; ?>
</select>
</td>
<td class="col-md-4">
<input type="submit" name="submit" id="submit" class="btn btn-sm btn-success">
</td>
</form>
NOTE: $payment_data is an array containing the table data with field names r_id, fy etc
I have two methods for this.
First method
Create a hidden field inside form element to store the value of id.Put form action null
<form id="dynamicForm" action="" method="post">
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>">
On submit you will get two values
if(isset($_POST['submit'])){
$id=$_POST['id'];
$r_id=$_POST['dynamicfy'];
header("location: client-detail-dynamic.php?id=" . $id . "&r_id=" . $r_id . "");
exit();
}
Second method use javascript
<select class="form-control" id="dynamicfy" name="dynamicfy" onchange="rdrt(this.value)">
<script>
function rdrt(str){
id=<?php echo $_GET['id']; ?>;
if(str!=""){
location.href="client-detail-dynamic.php?id=" + id + "&r_id=" + str;
}
}
</script>
Rather than changing the page from FORM ACTION what you can do is pick the values and set them in url passed to header:location.
try this.
``<?php
if(isset($_POST['submit'])
{
$option = $_POST['dynamicfy'];
$id = $_POST['id']
header('location: http://client-detail-dynamic.php?id=$id,r_id=$option');
}
?>
<form id="dynamicForm" action="" method="post">
<select class="form-control" id="dynamicfy" name="dynamicfy">
<?php
$j = 0;
foreach($payment_data as $pd):
?>
<option value="<?php echo $payment_data[$j]->r_id; ?>"><?php echo $payment_data[$j]->fy; ?></option>
<?php $j++; endforeach; ?>
</select>
</td>
<td class="col-md-4">
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>
<input type="submit" name="submit" id="submit" class="btn btn-sm btn-success">
" />
</td>
</form>
What you are trying to do is go somewhere based in the $_GET['id]. That's not possible server side as you have to FIRST make the request, then execute code. If your aren't trying to bring form data with you to this URL, then try this suggestion. However forget what I about not possible. you could do something like:
<?php
if(isset($_POST['submit-button'])) {
header("location: file.php?something=" . $_GET['id']);
}
// set the form action to nothing and add this to the same page the form is on
// and you can redirect based on the $_GET['id']
?>
To change value on selection of dropdown, You will need to use a jQuery on change of select box.
Please refer following code for same.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript">
jQuery(Document).ready(function() {
jQuery('#dynamicfy').change();
});
jQuery('#dynamicfy').change(function() {
jQuery('#dynamicForm').attr('action', 'client-detail-dynamic.php?id=' +<?php echo $_GET['id']; ?> + '&r_id=' + jQuery(this).val());
});
</script>
if you just want selected dropdown on the action page then You may also get selected dropdown on the action page with using $_POST['dynamicfy'] on action page "client-detail-dynamic.php"
This is my code for get database data to select box and i wanna get the seleceted value.I tries many ways but im missing something. help me
<form id="search" action="" method="post" >
<select name="owner" id="owner">
<?php
$sql = mysql_query("SELECT designation FROM designation");
while ($row = mysql_fetch_array($sql)){
echo '<option value="'.$row['designation'].'">'.$row['designation'].'</option>';
}
?>
</select>
<input type="submit" value="Search">
</form>
As you didn't specify an action for your form, the default will be to send the post values to the same page.
See this for more information about action value.
So, in the same page you have the form, you should add a
if(isset($_POST['owner']))
{
// Do some stuff
}
else
{
// Print the form
}
First make sure to include the action. Secondly to get a POST request of a select tag all you have to do is the following:
$_POST["owner"];
$_POST['owner'] contains the value of select box once you submit the form.And $_POST contains all the value of input elements you submitted via the form.if you print_r($_POST); it will show you all the values submitted through the form.
If you
echo $_POST['owner'];//Will display the value of your selected value in the select box.
<form id="search" action="" method="post" >
<select name="owner" id="owner">
<?php
$owner="rejayi"
$sql = mysql_query("SELECT designation FROM designation");
while ($row = mysql_fetch_array($sql)){
if($row['designation'] == $owner){
echo '<option value="'.$row['designation'].'" selected="selected">'.$row['designation'].'</option>';
}else{
echo '<option value="'.$row['designation'].'">'.$row['designation'].'</option>';
}
}
?>
</select>
<input type="submit" value="Search">
</form>
Put Double quotes (") outside and single quotes (') inside
eg:
echo "<option value='".$row['designation']."'>".$row['designation']."</option>";
How do I set up a table so that when the user clicks a row, the data from that row is sent to a PHP script which is loaded into a section on the website?
Basically, I want to partially-populate a data entry form, which I can then add more data to, and then submit the form.
I want to set up the JavaScript so that the click event loads the web form (easy) but this web form will INCLUDE some data from the record that was fetched.
So the order of the data flow would be:
Client-browser (user clicks on the row, JavaScript)
The data-attribute for that specific row is used to fetch data from the database associated with that attribute (php)
Present a web form
Here is the Javascript:
$(document).on('click', 'tr', function() {
var id = $(this).attr("data-recordId");
$('#section2').load('data_entry_form.php?id='+id);
});
Here is the PHP for the Web Form
<html>
<?php
$id = $_GET['id'];
$sql = ("
SELECT a_aif.aif_id, a_aif.fee_source_id, a_aif.company_name_per_sedar, a_aif.document_filing_date
FROM a_aif
LEFT JOIN a_aif_remaining
ON a_aif_remaining.aif_id = a_aif.aif_id
WHERE a_aif.aif_id = :id
ORDER BY aif_id DESC");
$sth = $dbh->prepare($sql);
$sth->execute();
?>
<?php echo $id; ?> <br>
<?php echo $row[fee_source_id]; ?> <br>
<?php echo $row[company_name_per_sedar]; ?> <br>
<?php echo $row[document_filing_date]; ?> <br>
<form>
<input type="date"> </input>
<br>
<input list="auditors" placeholder="Auditor" />
<datalist id="auditors">
<option value="/Foo" />
</datalist>
<br>
<input list="city" placeholder="City" />
<datalist id="city">
<option value="Bar" />
</datalist>
<br>
<input type="submit" name="submitbutton" id="submitbutton" value="Submit" />
</form>
</html>
QUESTIONS
what is the point of making a data-attribute equal to the first cell
of each row? Can't I just use the data in the first cell of each row
to use as the "id"?
If 1 is true, then I suppose I should be using the data element instead of the attr element?
the PHP is not working! I get this error "Fatal error: Call to a member function prepare() on a non-object.."
So many problems, do you know the basics of what you're doing here?
You Javascript is sending the record ID using the id= parameter, but your PHP is using $_GET['rowid']. You need to change one of them to match the other.
You need to assign it to a variable:
$id = $_GET['id'];
You need to change rowid in the SQL to :rowid, and prepare the statement:
$stmt = $dbh->prepare($sql);