Update MySQL table from Dropdown and Form - php

I'm posting this question up here because i'm a bit lost. This is my first time working on PHP and MySQL and I'm doing this for my uni assignment. I've managed to do all the CSS, Javascript and part of the PHP/MySQL requirements. The only thing that I have left is:
How do I get the value from my form that is generated through PHP and MYSQLquery and update the respective row of my MySQL table. I've attached an image to make it easier to understand.
The left side allows a user to search or display all orders. The PHP within will then call itself to update the table on the right. That part I managed to do it. But now part of my assignment requirement is:
The Manager should be able to ‘update’ the status of an order from a link
or button next to the order in the table, changing the status from
(pending | fulfilled | paid | archived). This could be done by returning
the record to a form in this webpage, to change and ‘confirm’ the change.
Now the table generated is actually a form. I've put some snippets of the codes here
echo "<form method=\"post\" id=\"regForm\" action=\"https://formtest.php\" name=\"editAll\">\n";
.......
echo "<td>", $row["order_status"],"</td>\n";
echo "<td><select name=\"change_orderstatus[]\" id=\"change_orderstatus[]\">
<option value=\"\" selected=\"selected\" >Please Select</option>
<option value=\"pending\">Pending</option>
<option value=\"Fulfilled\">Fulfilled</option>
<option value=\"Paid\">Paid</option>
<option value=\"Archived\">Archived</option>
<option value=\"Delete\">Delete</option>
</select></td>\n";
....
echo "<input type= \"submit\" value=\"Save changes\" id=\"commit\" name=\"editAllquery\" />";
If I put change_orderstatus[] it supposedly becomes an array but how do I make use of this to update my MySQL Table
I'm attaching another picture to show the bottom where I have the save changes button
Now I know that Stackoverflow is known for bashing people instead of helping because "all the noobs should learn their stuff and all" (this is from my personal experience) but I'm not asking you to write the codes for me or help me do my assignment.
I'm just asking you to point me to a direction where I can then do it on my own. That's all. I've tried to make my post as clear and as detailed as possible so that I don't get immediately down voted to oblivion.
If you could help me out, that would be great.
Thank you

First you'll need to output a form somewhere, I suggest making it like:
<form action="phphandler.php" id="form1" method="POST" style="display: none;">
<input type="hidden" name="id" id="inp1">
<input type="hidden" name="status" id="inp2">
</form>
Make onChange event for select :
<select onchange="event.preventDefault(); myFunction(1)" id="sel1">
myFunction(1) should be generated for each select with its unique id using php
Then function like:
function myFunction(id) {
document.getElementById('inp1').value = id;
document.getElementById('inp2').value = document.getElementById('sel1').value;
document.getElementById('form1').submit()
}
All ID's are generated by php except for inp1 and inp2 and form1

Related

Hide select option if already in DB

I don't know if what I want to do is possible (or too complicated).
I have a form that inserts the data into my DB. In it I have a select. I want to hide or remove the options that are already in my DB (so the user can't see them).
Every user has his own .php page where I edit the form with new options after the old ones are used.
PHP (In the same page, above the form):
<?php
if ($registro != "") {
include("conexao.php");
$conexao = #mysql_connect($host,$user,$pass)
or die("Cadastro");
$query = "INSERT INTO certificados VALUES ('$id','$registro','$name', now() )";
mysql_select_db($db);
mysql_query($query,$conexao);
}
else {
?>
HTML:
<form name="inserir" action="<? echo $PHP_SELF; ?>" method="post">
<select name="registro" id="registro">
<option value="">Select one</option>
<option value="11891">11891</option>
<option value="11892">11892</option>
<option value="11893">11893</option>
<option value="11894">11894</option>
<option value="11895">11895</option>
</select>
<input type="text" name="name" placeholder="Name">
<button type="submit" id="submit" name="B3">Send</button>
</form>
Edit 1: I'll try to explain better what I want to do.
Imagine that the user selected the first option value "11891" and submitted the form (thus inserting this value in the DB). After the page reloads, this value is no longer listed for him in the select. So based on the form that I posted here, now he would only have the options "11892", "11893", "11894" and "11895".
PS: Sorry for any mistakes or use of outdated parameters. My father wrote this code (he has some knowledge of php). In my case, my knowledge is almost nonexistent.
Thank you all for the help.
You should have 2 more tables. One 'tb_options' where you retrieve all the options. Another one 'tb_users_options' where you store all option based on user ID. So when retrieving the options from tb_options you select only those not present in tb_users_options Taking into account the user ID. This is a logic matter.

Drop down list in PHP that filters MySQL data

I am trying to print out the data I have in a MySQL table, but I want them filtered based on the values of a column. I want these values to be displayed in a drop down list, and after one of them is chosen I wont the program to print out all the records that fill that requirement. To make it more clear I want books that are in the library to be printed, based on the category that is selected from the drop down menu. I have done some research and I've modified some codes. But it's my very first time coding in PHP and I have clearly done something wrong. The code I try to execute is this:
<form id="form1" name="form1" method="POST" action="">
Fusha e kërkimit:
<select Name='NEW'>
<option value="">---Zgjidh---</option>
<?
mysql_connect('localhost', '<password>', '<user>');
mysql_select_db("<mysql_db>");
if (isset ($select) && $select!=""){
$select=$_POST['NEW'];
}
?>
<?
$list=mysql_query("SELECT * FROM 'arkiva' ORDER BY 'Fusha'");
while($row_list=mysql_fetch_assoc($list)){
?>
<option value="<?php echo htmlspecialchars($row_list['Fusha']);?>">
<?php echo htmlspecialchars($row_list['Fusha']); ?>
</option>
<?
}?>
</select>
<input type="submit" name="Submit" value="Dërgo" />
</form>
but this only prints out the box of the drop down with no actual values, and I have a missing piece of code that prints out after the selection.
Thank you so much in advance!
Your query isn't ordering correctly due to syntax. Adding backticks should help:
$list=mysqli_query("SELECT * FROM `arkiva` ORDER BY `Fusha`");
This answer addresses what backticks do in your query.
Still not able to make it show my fields in the dropdown. I have temporarily solved this by manually creating the fields of the dropdown. Bur it certainly gets very confusing and long with the growth of my database. So I still need to work this out for a permanent solution. Anyways thank you both for the help

Calling PHP function with value from HTML Dropdown Menu

I'm in the midst of creating a website that includes shopping cart functionality and have run into an issue with passing variables back-and-forth from HTML/Javascript to PHP. I understand that these languages are fundamentally different and was hoping someone could provide some guidance. I've seen several questions on similar topics, but unfortunately have yet to find a solution that works for my situation.
I have created a multidimensional array of products in php and would like to capture the value from a dropdown menu to call a function in which the value of the dropdown corresponds to a row in the product array. My list of products appear in a HTML table. I have experimented with $_GET and $_POST, but haven't had any luck. Plus I would like to avoid adding a submit button as the print_wp_cart_button_for_product function outputs an add to cart button. The print_wp_cart_button_for_product also creates the shopping cart on the sidebar.
<TD>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="get">
<select id="productcat1" name="productcat1">
<option value="$">--Please Select--</option>
<option value="1">Product # 1 - $1.99</option>
<option value="2">Product # 2 - $1.99</option>
<option value="3">Product # 3 - $9.99</option>
<option value="4">Product # 4 - $9.99</option>
</select>
</form>
</TD>
<TD>
<?php $currentrow = 0; ?>
<?php $currentrow = $_GET["productcat1"]; ?>
<?php echo print_wp_cart_button_for_product($products[$currentrow]["Product"], $products[$currentrow]["Price"]); ?>
</TD>
Since PHP is a server side language, it has no way of knowing what is happening in the client (i.e. what is happening live in the users browser, such as which dropdown they have selected). You will have to use javascript/ajax, either to run your function entirely, or to communicate the selected option back to the server to run the PHP function. Alternatively, you can communicate with the server without javascript/ajax by submitting the form, but you said you don't want to do that.
Good luck!

PHP Dropdown button, MySQL update

I've been looking around on SO and I don't -think- this is something that's up here, either that or it's something I've missed. If so, please point me there and I'll simply mark this question as answered.
I have a completely working PHP form where my users input information into input boxes and there's a button at the bottom that submits the information to the MySQL database, so that part works perfectly. What I'm trying to do now is this: have 2 drop down menu's, each one with a static list of choices and I'd like those choices to also get sent to the database.
For instance, currently in my form I have First Name (frame) and Last Name (lname) that the user can input and then if I query the database it spits out First Name and Last Name perfectly. So now I'd like to add to my form a drop down box where the user can pick, for example, Boy or Girl, and then after doing that click the submit button that's already there (I don't want the drop-down to submit the data, and I don't want the drop-down to be populated from the database.)
I'm guessing I need to use Javascript for this part? But I really don't know.
Any advice would be appreciated.
<html>
<head>
<title>MyForm</title>
</head>
<body>
<form id="form" name="form" action="" method="post">
<Label>First Name/Organization</label>
<input type="text" name="firstname" value="<?php echo $firstname; ?>"/>
<input type="submit" name="submit" value="Add entry">
</form>
</body>
</html>
So, as part of the same form, you simply treat it just like the input boxes. Here's an example:
If the dropdown is named:
<select name="gender">
<option value="boy">Boy</option>
<option value="girl">Girl</option>
</select>
Then in your php, you would simply get the value for gender:
$gender = $_POST["gender"];
And add to your SQL statement where you are saving the first/last name the additional field and value for gender.
Of course, you would have to first modify the table to have the column for gender....
Hope that helps!
It sound s like you just want a simple html drop down as part if your form. Just use
<select name = 'gender'>
<option value = 'boy'> Boy </option>
< option value = 'girl'> Girl </option>
</select>
Then access $_POST['gender'] in the receiving script to put it into the database (after validation of course)

PHP Drop down List . Saving data to post on other pages in form

I have been searching for an answer to this for the last two days.
I am trying to add a list of manufacturers to an existing form in php.
This worked out ok and I got it to work somewhat.
Here is the problem when the user makes the selection and submits the form
it takes them to another page that shows them a preview of their post.
In this preview it show them the manufacturer they selected. But when they finalize the the
post it doesn't show using the same code.
I know that their must be a simple way of doing this. I am not great with php but I have put forth a valiant effort to get this done. I am stumped please help.
Here is my code.
HTML:
<select action="select.php" name="manufacturer">
<option value="Lincoln">Lincoln</option>
<option value="Example">Example</option>
</select>
PHP:
<?php
if(isset($_POST['manufactuer']))
echo "Manufacturer: ".$_POST['manufacturer'];
else {?>
I call to the selection with this.
<?php echo "Manufacturer: ".$_POST['manufacturer']; ?>
How do I show this when the user posts and if the same user posts again or a different user posts to show their selections for those specific posts.
posts
You can pass your variable on from your "review" page to your next page with a hidden field.
<form><input type='hidden' value='<?php echo $_POST['manufacturer']; ?>' name='manufacturer' /></form>
Just set the selected="selected" option in the selected select with php. That makes:
<option value="Lincoln" selected="selected">Lincoln</option>

Categories