I am building an invoice system for a client and would like to give them the ability to change the order status and payment status of the order by selecting them from a select dropdown.
The forms
<form action="changestatus.php" method="post">
<input type="hidden" name="status" value="order"/>
<input type="hidden" name="orderid" value="33"/>
<select name="orderstatus">
<option value="Complete">Complete</option>
<option value="Incomplete">Incomplete</option>
</select>
</form>
<form action="changestatus.php" method="post">
<input type="hidden" name="status" value="payment"/>
<input type="hidden" name="orderid" value="33"/>
<select name="paymentstatus">
<option value="Complete">Complete</option>
<option value="Incomplete">Incomplete</option>
</select>
</form>
changestatus.php
<?php
switch($_POST['status'])
{
case"payment":
mysql_query("UPDATE orders SET payment_status = '$_POST[paymentstatus]' WHERE ID = '$_POST[orderid]'")or die(mysql_error());
break;
case"order":
mysql_query("UPDATE orders SET order_status = '$_POST[orderstatus]' WHERE ID = '$_POST[orderid]'")or die(mysql_error());
break;
}
?>
I think I can use jquery.post() (http://api.jquery.com/jQuery.post/) but I have no idea how to implement it.
/////EDIT
OK i found a solution to this.
I changed "changestatus.php" to accept GET variables
<?php
include"../inc/config.php";
switch($_GET['status'])
{
case"payment":
$query = mysql_query("UPDATE orders SET payment_status = '$_GET[paymentstatus]' WHERE ID = '$_GET[ID]'")or die(mysql_error());
if($query){echo"Saved";}else{echo"Not Saved";};
break;
case"order":
$query = mysql_query("UPDATE orders SET orderStatus = '$_GET[orderstatus]' wHERE ID = '$_GET[ID]'")or die(mysql_error());
if($query){echo"Saved";}else{echo"Not Saved";};
break;
}
?>
Then changed the forms to
<select name="orderstatus">
<option value="Complete">Complete</option>
<option value="Incomplete">Incomplete</option>
</select>
<select name="paymentstatus">
<option value="Complete">Complete</option>
<option value="Incomplete">Incomplete</option>
</select>
and finally the javascript
<script>
$(document).ready(function() {
$('#paymentstatus').change(function() {
url = "changestatus.php?ID=<?php echo $_GET['ORDERID'];?>&status=payment&paymentstatus="+$('#paymentstatus').val();
$("#payment_status_result").load(url)
});
$('#orderstatus').change(function() {
url = "changestatus.php?ID=<?php echo $_GET['ORDERID'];?>&status=order&orderstatus="+$('#orderstatus').val();
$("#order_status_result").load(url)
});
});
</script>
See http://api.jquery.com/change/ to find out how to trigger javascript when a drop-down menu is changed.
As for the jquery post, the primary reason for using this is for making an AJAX call. Whatever you do, you don't want to ever ignore the result. Your script should always return an error or success message.
First give your forms and selects ids like so:
<form action="changestatus.php" method="post" id="orderStatusForm">
<form action="changestatus.php" method="post" id="paymentStatusForm">
<select name="orderstatus" id="orderstatus">
<select name="paymentstatus" id="paymentstatus">
Then add a change observer in your script:
var changeOrderStatus = function(){
$.post("changestatus.php", $("#orderStatusForm").serialize());
}
var changePaymentStatus = function(){
$.post("changestatus.php", $("#paymentStatusForm").serialize());
}
$('#orderstatus').change(changeOrderStatus);
$('#paymentstatus').change(changePaymentStatus);
That should get you started, then read a bit more about the jQuery AJAX functions so you can handle the response from the server.
sure, use change() like this:
$("#orderStatus").change(function() { /* post() your form here */ });
and add id="orderStatus" to the order status dropdown
Related
I have a dropdownlist populated by a MySql database that shows the titles of books stored in my database.
<select id="titles">
<option value="emp" selected>Choose the title</option>
<?php
//drop down list populated by mysql database
$sql = mysql_query("SELECT title FROM book");
while ($row = mysql_fetch_array($sql))
{
echo '<option value="'.$row['title'].'">'.$row['title'].'</option>';
}
?>
I want the option I choose to send it to another php page search.php
This search.php I want it to get the title and search for this specific book details.(title, price, author.... etc) .I tried to do it with but it ruins the page.
Add below code in form that should work for you.
<form action='search.php' method='post'>
<select id="titles" name='titles'>
<option value="emp" selected>Choose the title</option>
<?php
//drop down list populated by mysql database
$sql = mysql_query("SELECT title FROM book");
while ($row = mysql_fetch_array($sql))
{
echo '<option value="'.$row['title'].'">'.$row['title'].'</option>';
}
?>
<input type='submit' value='submit'>
</form>
in search.php:
$title = $_POST['titles'];
You just need to add the form above the select tag and need to give the NAME attribute in the select tag to post the data on another page. You can try with the following code:
<form method="post" action="search.php">
<select id="titles" name="titles">
<option value="emp" selected>Choose the title</option>
<?php
//drop down list populated by mysql database
$sql = mysql_query("SELECT title FROM book");
while ($row = mysql_fetch_array($sql))
{
echo '<option value="'.$row['title'].'">'.$row['title'].'</option>';
}
?>
</select>
<input type="submit" name="submit"/>
</form>
and on the search.php page, you can get the value of the dropdown by this:
$title = $_POST['titles'];
Try as below :
<form method="post" action="YOURPAGEPATH">
<select id="titles">
<option value="emp" selected>Choose the title</option>
<?php
//drop down list populated by mysql database
$sql = mysql_query("SELECT title FROM book");
while ($row = mysql_fetch_array($sql))
{
echo '<option value="'.$row['title'].'">'.$row['title'].'</option>';
}
?>
</select>
<input type="submit" name="submit"/>
</form>
Without submit button :
<form method="post">
<select id="titles" onchange="this.form.submit();">
<option value="emp" selected>Choose the title</option>
<?php
//drop down list populated by mysql database
$sql = mysql_query("SELECT title FROM book");
while ($row = mysql_fetch_array($sql))
{
echo '<option value="'.$row['title'].'">'.$row['title'].'</option>';
}
?>
</select>
</form>
Surround that with a form with the appropriate action and add a submit button. Otherwise use something like jQuery to listen for the value of that to change and submit the form.
For example:
<form action="search.php" method="GET">
<select id="titles" name="title">
<?php /* put your stuff here */ ?>
</select>
</form>
And then in jQuery:
$(function(){
$('#titles').on('change', function(){
$(this).closest('form').submit();
});
});
Or you could go real old-school and attach the event listener to the select like this:
<form action="search.php" method="GET">
<select id="titles" name="title" onchange="this.parentNode.submit()">
<?php /* put your stuff here */ ?>
</select>
</form>
Just in case if you don't want to use the Submit Button
<script language="Javascript">
function books(book)
{
var url="http://www.example.com/search.php/?q="+book;
window.open(url, "_self");
}
</script>
<select id="titles">
<option value="emp" selected>Choose the title</option>
<?php
//drop down list populated by mysql database
$sql = mysql_query("SELECT title FROM book");
while ($row = mysql_fetch_array($sql))
{
echo '<option onClick="books('.$row['title'].')" value="'.$row['title'].'">'.$row['title'].'</option>';
}
?>
Here the list will call the Books function on Click and pass the arguments to the function which will redirect you to search.php
To retrieve the book name use
$_GET["q"]
Change the URL as required.
And if the problem is solved don't forget to Mark the answer.
I tried to find ways how to update my database for hours now and it's hurting my brain.
Please, can anyone show me how to update data inside a database by using <select>?
If I select 'Checked-Out' in the form and then click update the StatusID in the database will be updated to '2' but I just can't seem to work it.
<?php
include 'connection.php';
if(isset($_POST['submit'])){
$_var1 = $_POST['status'];
$query3 = mysql_query("UPDATE reservation SET ReservationStatusID = '$_var1' WHERE `ReservationID` = '$id' ");
if($query3)
{
header('location:viewReservedroomsGuest1.php');
}
else
{
echo "Error";
}
}
?>
Here's my select.
<select id="status">
<option value="1">Checked-In</option>
<option value="2">Checked-Out</option>
<option value="3">Pending</option>
<option value="4">Cancelled</option>
</select>
<input type="submit" name="submit" value="Update" />
I'm sorry, I don't make any sense. My brain is tired out already.
<form action="sampleupdate.php" method="post">
<select id="status" name="status">
<option value="1">Checked-In</option>
<option value="2">Checked-Out</option>
<option value="3">Pending</option>
<option value="4">Cancelled</option>
</select>
<input type="submit" name="submit" value="Update" />
</form>
in sampleupdate.php
$val = $_POST['status'];
// and write and execute the update Query here..
Put the select in a <form> with method="post"
Give the select a name attribute
Read the data from $_POST['the name you used']
Use the database API of your choice
I need to show the item_id when I click one of the item_name list in the dropdown. And when I click submit, the item_name and item_id got by $_POST. Can javascript use to do that?
<?php
$brg="SELECT item_id, item_name FROM tblItem";
$b=mysql_query($brg);
?>
<td align="center">
<select name="item_name">
<?php while($br=mysql_fetch_array($b)){ ?>
<option value=<?echo $br[0].'_'.$br[1]?> selected>
<?php echo $br[1]; ?>
</option>
<?php } ?>
</select>
</td>
Jup, that's possible with Javascript.
Your HTML:
<select name="item_name">
<option value="1_Cheese">Cheese</option>
<option value="2_Cars">Cars</option>
<option value="3_Planes">Planes</option>
</select>
<input type="button" onclick="alert(getSelectedValue())" value="Get Select Id" />
And your Javascript, to access the ID from the options value:
function getSelectedValue()
{
var sel = document.getElementsByName('item_name')[0];
return sel.options[sel.selectedIndex].value.split("_")[0];
}
See this fiddle: http://jsfiddle.net/acz6Q/
It would be a little easier with jQuery (also appended an event handler):
$(document).ready(function()
{
$("select[name=item_name]").on("change", function()
{
alert("new item id = " + $(this).val().split("_")[0]);
});
});
And here is the jQuery version fiddle: http://jsfiddle.net/acz6Q/1/
Hope this helps!
Try this:
<form action="" method="post">
<input type="hidden" id="item_id" name="item_id" />
<input type="hidden" id="item_name" name="item_name" />
<select id="my_select" name="my_select">
<option value="item_id_1">item_name_1</option>
<option value="item_id_2">item_name_2</option>
<option value="item_id_3">item_name_3</option>
</select>
</form>
<script type="text/javascript">
$(document).ready(function() {
$("#my_select").change(function() {
$("#item_id").val($("#my_select").val());
$("#item_name").val($("#my_select option:selected").text());
});
});
</script>
Note that you need to include the jQuery library first. Also you can see how it works from here jsfiddle.net/2FsNP/3. And with this method you don't need to combine between value and text of the option tag with the underscore.
I asked this question previously but i think i made it too complex, I want to assign a query if a value from a list is selected.
<form id="filter" name="filter" method="post" action="">
<select>
<option value="petrol">Petrol</option>
<option value="diesel">Diesel</option>
</select>
<p><input name="filter" type="button" value="Filter" /></p>
</form>
What i want to achieve is if user selects petrol the run the following query
$query = mysql_query("SELECT fuel_type from car WHERE fuel_type = 'petrol'");
<form id="filter" name="filter" method="post" action="">
<select id="college" name="fuel" onchange="changeValue();">
<option value="petrol">Petrol</option>
<option value="diesel">Diesel</option>
</select>
<p><input name="filter" type="button" value="Filter" /></p>
</form>
If you want to post the value in form submit use like this
$ctext = $_POST['fuel'];
$list = mysql_query("SELECT fuel_type from car WHERE fuel_type ='$ctext'");
$row = mysql_fetch_array($list);
//process $row
If you don't want to submit the form, you can use jquery in onchange event...
js code
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
function changeValue() {
var ctext = $("#college option:selected").val();
$.ajax({ url: "yourfile.php",
data: {"ctext":ctext},
type: 'post',
success: function(output) {
//display output here
}
});
}
yourfile.php
<?php
//connect to db
$ctext = $_POST['ctext'];
$list = mysql_query("SELECT fuel_type from car WHERE fuel_type ='$ctext'");
$row = mysql_fetch_array($list);
echo $row['fuel_type'];
?>
Give your select tag a name:
<select name="fuel">
<option value="petrol">Petrol</option>
<option value="diesel">Diesel</option>
</select>
Then in your PHP you can do something simple like:
<?php
if(isset($_POST['cmdFilter']))
{
echo "You selected the fuel type: " . $_POST['fuel'];
}
?>
You can also set the select to have an id instead and use JQuery to send an ajax request passing it the value by giving selecting it by $("#myIdforSelect").val()
P.S
Be sure to sanitise your input ;)
Although I would also suggest the AJAX method, I just want to make it clear that you do not HAVE to use AJAX to accomplish this.
If your filter options are not too complex or many, then you can simply do a pull for all available options for any level of the filter tree on load and hide them initially. Then, as the user drills down into each section, you show them the next one accordingly (via javscript). In the end you can just submit a regular form with all the selected form fields via GET or POST.
This can effectively save bandwidth by running less calls to the server also :)
BUT if your choice tree gets too complicated, it might be better to just run AJAX request on user interaction.
Can someone show me how to make a If statement where if a drop down does not equal the default value of 0 (in my case) to automatically submit the form if possible?
session_start();
$current = isset($_SESSION['ClientNamefour']) ? $_SESSION['ClientNamefour'] : 0;
while ($row = mysql_fetch_array($result)) {
$id = $row["Client_Code"];
$thing = $row["Client_Full_Name"];
$value = "$id, $thing";
$sel=($id==$current)?'SELECTED':'';
$options4.="<OPTION $sel VALUE=\"$value\">$thing</option>";
}
?>
<FORM name="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<SELECT NAME="ClientNamefour" OnChange="this.form.submit()"
)">
<OPTION VALUE=0>Client
<?php echo $options4?>
</SELECT>
</FORM>
Use the onchange event and some JavaScript. In general your generated HTML should look something like this:
<form id="form" name="form" method="POST" action="http://example/script.php">
<select id="select" name="select" onChange="document.getElementById('form').submit();">
<option value="0" selected="selected">0</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
</form>
The onchange event is only fired when you select an unselected option, so no additional checks are required.
Compared with your solution:
the id is missing in your <form> tag
you need closing </option> tags
you'd probably need to change the JavaScript in the onchange attribute of the <select> tag
My understanding is that you can't replicate someone pushing a "submit" button using a server side language such as PHP. You could use javascript to submit the form if the user changes it (to something other than 0)
However, it looks like you only want the form to display if the SQL query returns a result that isn't 0?
In that case I'd do something like this...
Run your SQL Query
Code something like this:
if($option != 0){
//Function to do whatever happens after the form is sent (add to a database etc)
}
else{
//display the form
}
This is really sort of a javascript question... add an id attribute to your form like <form id="myfrm"...>, then after the </form> put:
<?php
if(((int) $_SESSION["your_session_variable"]) != 0){
?>
<SCRIPT language="javascript">
document.getElmenetById("myfrm").submit();
</SCRIPT>
<?ph } ?>