Dependent dropdown PHP - php

Hello I want to show a table that have all the values from a mysql table but depending on the values that I will select from a dropdown menu. For example the dropdown list menu have a value named open. I just want to see the rows from the table that have that status. I will need to use Ajax for this?
Here is my code?
$status = $_POST['TipoStatus'];
echo ' Create Service ';
echo '</br>';
echo '</tr><tr><td><label for="TipoStatus"> Status:</label></td><td>';
$query = "SELECT TipoStatus FROM status"; // First Remar
$result = queryMysql($query);
if (!queryMysql($query)) {
echo "Query fail: $query<br />" .
mysql_error() . "<br /><br />";
}
else
{
echo '<select name = "TipoStatus" size = "1">'; // or name="toinsert[]"
// echo '<option value="none" selected="selected">None</option>';
while ($row_1 = mysql_fetch_array($result)) {
echo '<option value="' . htmlspecialchars($row_1['TipoStatus']) . ' selected="$row_1[9]" >' // Third remark
. htmlspecialchars($row_1['TipoStatus'])
. '</option>';
}
echo '</select>';
echo '</p>';
}
echo '<table border="1" >';
echo '<tr>';
echo '</br>';
echo '<th> Service ID</th>';
echo '<th>Title</th>';
echo '<th>Description</th>';
echo '<th>Notes</th>';
echo '<th>Submit By</th>';
echo '<th>Assigned Employee</th>';
echo '<th>Assigned Group</th>';
echo '<th>Category</th>';
echo '<th>Status</th>';
echo '<th>Urgency</th>';
echo '<th>Customer</th>';
echo '<th>Day Created</th>';
echo '</tr>';
$query = ("SELECT ServiceID, Title, Description, Notes, SubmitBy, AssignedEmp, " .
"AssignedGroup, NameCategory, TipoStatus, TiposUrgencia, CustomerName, DayCreation FROM Service where TipoStatus = '$status' ");
$result = queryMysql($query);
echo 'resultado' . mysql_num_rows($result);
while ($row = mysql_fetch_assoc($result)) {
echo '<tr>';
echo '<td> '.$row['ServiceID'] .' </td>';
echo '<td>' .$row['Title']. ' </td>';
echo '<td>'.$row['Description'].'</td>';
echo '<td>'.$row['Notes'].'</td>';
echo '<td>'.$row['SubmitBy'].'</td>';
echo '<td>'.$row['AssignedEmp'].'</td>';
echo '<td>'.$row['AssignedGroup'].'</td>';
echo '<td>'.$row['NameCategory'].'</td>';
echo '<td>'.$row['TipoStatus'].'</td>';
echo '<td>'.$row['TiposUrgencia'].'</td>';
echo '<td>'.$row['CustomerName'].'</td>';
echo '<td>'.$row['DayCreation'].'</td>';
echo '</tr>';
}
mysqli_free_result($result);
echo $ticket_select;`enter code here`
echo '</table>';
echo '<form method = "post" action "rnseetickets.php">';
?>

Your code is a little meandering, and I can't quite tell if you're running this from the command line or as a web page, but it seems like you're wanting to filter a SQL result with a drop-down of options? That can be done with a simple form (if you don't mind a page refresh) or with AJAX (if you want it to filter without a page refresh):
// Run SQL
$sql = "SELECT TipoStatus FROM status";
if (!empty($_GET['status'])) {
$sql .= "WHERE `value`=\"".addslashes($_GET['status'])."\"";
}
$result = queryMysql($sql);
// Filter form
echo "<form action=\"".$_SERVER['PHP_SELF']."\" method="get">";
echo "<select name=\"status\"><option>open</option><option>other option</option></select>";
echo "<input type=\"submit\" value=\"Filter\">";
echo "</form>";
// Table
// As you already have it

Greetings. You can submit a forum on the onchange event of the dropdown box.
Check this link:
javascript onchange submit
You can find more examples by searching 'javascript onchange submit'
Now when the user selects something in the drop down box the form will submit and you can use your php code do filter the results.

Related

How do I obtain the value of the option that was chosen in a select from?

In the PHP code below I have created a form that is used to create a drop down list.
<?php
echo "<body>";
echo "<div id='network_name' class='col-md-3'>";
echo "<h2> Agency Network </h2>";
echo "<form action='droplistpop.php' method='post'>";
echo "<select name='network'>";
while($result1 = mysqli_fetch_assoc($result)) {
unset($network_id, $network_name);
$network_id = $network_name['network_id'];
$network_name = $result1['network_code'];
echo '<option name="entry" value="' . $network_id . '">' . $network_name . '</option>';
$network_chosen = $network_id;
}
echo "</select>";
echo "<input name='submit' type='submit' value='Send' />";
echo "</form>";
echo "</div>";
?>
This code works as desired. It populates a drop down list based on the results of a database query. After the form has been submitted, I want to obtain the option that was selected and use it in another query. After the submit, I print the contents of the $_POST variable using:
print_r($_POST);
and my results are as follow:
Array ( [network] => [submit] => Send )
I want to get the value that was selected for network but it appears to be blank. Can anyone tell me what I'm doing wrong.
By the way, I am really new at coding and this is my first time using SO so please excuse any usage errors on my part. Thanks.
$network_id = $result1['network_id'];
instead of
$network_id = $network_name['network_id']
Output like this:
Array ( [network] => 2 [submit] => Send )
You are setting a wrong value to network_id, that's why it's output is blank. Also, the 'name' attribute on option tags is not needed. Try the changes below.
<?php
echo "<body>";
echo "<div id='network_name' class='col-md-3'>";
echo "<h2> Agency Network </h2>";
echo "<form action='droplistpop.php' method='post'>";
echo "<select name='network'>";
while($result1 = mysqli_fetch_assoc($result)) {
unset($network_id, $network_name);
$network_id = $result1['network_id'];
$network_name = $result1['network_code'];
echo '<option value="' . $network_id . '">' . $network_name . '</option>';
$network_chosen = $network_id;
}
echo "</select>";
echo "<input name='submit' type='submit' value='Send' />";
echo "</form>";
echo "</div>";
?>

In PHP and MySQL, how to view multiple order information in the other page

I've learned a lot from this question, on how to send multiple order information on the database. In my table rows of "order.php" is composed of rows about order information sent by the customers. My code is only for single order only. But I want to view in the other page the multiple orders sent by one customer.
Here is my code for "order.php"
<?php
session_start();
$conn = mysqli_connect('localhost','root','','sampsix');
if(mysqli_connect_errno()){
echo 'Failed to connect: '.mysqli_connect_error();
}
if(isset($_POST['delete'])){
$DeleteQuery = "DELETE FROM orders WHERE id='$_POST[hidden]'";
mysqli_query($conn,$DeleteQuery);
}
if(isset($_POST['view'])){
header('Location: view_order.php');
}
$query = "SELECT * FROM orders ORDER BY id";
$results = mysqli_query($conn,$query);
echo '<table border="1">';
echo '<tr>';
echo '<th>ID</th>';
echo '<th>Firstame</th>';
echo '<th>Lastname</th>';
echo '<th>Email</th>';
echo '<th>Order Name</th>';
echo '<th>Order Code</th>';
echo '<th>Order Qty</th>';
echo '<th>Sub Total</th>';
echo '</tr>';
while($orderData = mysqli_fetch_array($results)){
echo '<form action="order.php" method="POST">';
echo '<tr>';
echo '<td>'.$orderData['id'].'</td>';
echo '<td>'.$orderData['firstname'].'</td>';
echo '<td>'.$orderData['lastname'].'</td>';
echo '<td>'.$orderData['email'].'</td>';
echo '<td>'.$orderData['ordername'].'</td>';
echo '<td>'.$orderData['ordercode'].'</td>';
echo '<td>'.$orderData['orderqty'].'</td>';
echo '<td>'.$orderData['subtotal'].'</td>';
echo '<td><input type="hidden" name="hidden" value="'.$orderData['id'].'"></td>';
echo '<td><input type="submit" name="delete" value="Delete"></td>';
echo '</form>';
echo "<td><a href='view_order.php?id=".$orderData['id']."'>View</a></td>";
echo '</tr>';
}
echo '</table>';
mysqli_close($conn);
?>
And here is my "view_order.php" where in the order information is in there:
<?php
include_once('config.php');
if(isset($_GET['id'])){
$id = $_GET['id'];
$query = $mysqli->query("SELECT id,firstname,lastname,email,ordername,ordercode,orderqty,subtotal FROM orders WHERE id='$id'");
if($query){
while($obj = $query->fetch_object()){
echo 'ID: '.$obj->id;
echo 'Firstname: '.$obj->firstname;
echo 'Lastname: '.$obj->lastname;
echo 'Email: '.$obj->email;
echo 'Order Name: '.$obj->ordername;
echo 'Order Code: '.$obj->ordercode;
echo 'Order Qty: '.$obj->orderqty;
echo 'Sub total: '.$obj->subtotal;
}
}
}
?>
This code above also execute single order only. I just thinking what if the customers has multiple order and I want to view it all in the other page.
Now you use the id as identifier which refers to just one order. If you want all orders of a customer you should select by the identificator of the customer. In your case i think it is firstname and lastname. You should replace the id with firstname and lastname. You will get something like this:
<?php
session_start();
$conn = mysqli_connect('localhost','root','','sampsix');
if(mysqli_connect_errno()){
echo 'Failed to connect: '.mysqli_connect_error();
}
if(isset($_POST['delete'])){
$DeleteQuery = "DELETE FROM orders WHERE id='$_POST[hidden]'";
mysqli_query($conn,$DeleteQuery);
}
if(isset($_POST['view'])){
header('Location: view_order.php');
}
$query = "SELECT * FROM orders ORDER BY id";
$results = mysqli_query($conn,$query);
echo '<table border="1">';
echo '<tr>';
echo '<th>ID</th>';
echo '<th>Firstame</th>';
echo '<th>Lastname</th>';
echo '<th>Email</th>';
echo '<th>Order Name</th>';
echo '<th>Order Code</th>';
echo '<th>Order Qty</th>';
echo '<th>Sub Total</th>';
echo '</tr>';
while($orderData = mysqli_fetch_array($results)){
echo '<form action="order.php" method="POST">';
echo '<tr>';
echo '<td>'.$orderData['id'].'</td>';
echo '<td>'.$orderData['firstname'].'</td>';
echo '<td>'.$orderData['lastname'].'</td>';
echo '<td>'.$orderData['email'].'</td>';
echo '<td>'.$orderData['ordername'].'</td>';
echo '<td>'.$orderData['ordercode'].'</td>';
echo '<td>'.$orderData['orderqty'].'</td>';
echo '<td>'.$orderData['subtotal'].'</td>';
echo '<td><input type="hidden" name="hidden" value="'.$orderData['id'].'"></td>';
echo '<td><input type="submit" name="delete" value="Delete"></td>';
echo '</form>';
echo "<td><a href='view_order.php?firstname=".$orderData['firstname']."&lastname=".$orderData['lastname']."'>View</a></td>";
echo '</tr>';
}
echo '</table>';
mysqli_close($conn);
?>
And the view page:
<?php
include_once('config.php');
if(isset($_GET['firstname'])){
$firstname = $_GET['firstname'];
if(isset($_GET['lastname'])){
$lastname = $_GET['lastname'];
$query = $mysqli->query("SELECT id,firstname,lastname,email,ordername,ordercode,orderqty,subtotal FROM orders WHERE firstname='$firstname' and lastname='$lastname'");
if($query){
while($obj = $query->fetch_object()){
echo 'ID: '.$obj->id;
echo 'Firstname: '.$obj->firstname;
echo 'Lastname: '.$obj->lastname;
echo 'Email: '.$obj->email;
echo 'Order Name: '.$obj->ordername;
echo 'Order Code: '.$obj->ordercode;
echo 'Order Qty: '.$obj->orderqty;
echo 'Sub total: '.$obj->subtotal;
}
}
}
?>
Note that this structure isn't the best solution. I would store my customers in another table, because what happens if two people have the same first- and lastname?
In your other question you also create a row for each product in your order table. If you want to do it well you should create another table like order_rules and store your products in that table with an order id. You should normalize your tables. I think this is a good description and tutorial about normalizing tables.
Please note this code is not safe to use - it contains a number of SQL injection vulnerabilities. It has just been amended into a working state from the code in the original post.

How to get table column data when a button is clicked

Assume that I have a directory page that is generated by PHP. Each row data is from the database.
echo "<form action=\"reservation.php\" method=\"post\">";
for ($i=0; $i <$num_results; $i++) {
$row = $result->fetch_assoc();
if ( stripslashes($row['gymid']) == !0) {
echo "<tr>". PHP_EOL;
echo "<td>".stripslashes($i+1)."</td>". PHP_EOL;
echo "<td align=\"center\">".stripslashes($row['gymname'])."</td>". PHP_EOL;
echo "<td>".stripslashes($row['address'])."</td>". PHP_EOL;
echo "<td align=\"center\">".stripslashes($row['location'])."</td>". PHP_EOL;
echo "<td align=\"center\">".stripslashes($row['operatinghours'])."</td>". PHP_EOL;
echo "<td align=\"center\"><input type= \"submit\" id =\"gym".stripslashes($row['gymid'])."\" value=\"BOOK NOW\" class=\"bookbutton\" onClick=\"reply_click(this.id)\"</td>". PHP_EOL;
//echo "<td align=\"center\"><input type=\"submit\" name=\"gyminfo\" class=\"bookbutton\" value=\"".stripslashes($row['gymid'])."\" >BOOK NOW! </td>". PHP_EOL;
echo "</tr>". PHP_EOL;
}
}
echo "</form>";
So i have a "BOOK NOW" button for each row with unique id (gymxxxx). Upon clicking that button, the page will be redirect to reservation.php where there are a few dropdown boxes with gymname, location, timeslot and so on.
The following shows the dropdown box for gymname:
<label>Gym Name: </label>
<form id="gymInfoForm" method = "post" action = "reservation.php">
<select name="gymSelect"onchange="if (this.selectedIndex) formSubmit();">
<?php
$data = $_POST['gymSelect'];
echo "Data = " . $data;
$query = "SELECT * FROM gymname";
$result = $db->query($query);
$resultNum = $result->num_rows;
echo '<option value="NULL" >Please choose a gym</option>';
for($i = 0; $i < $resultNum; $i++)
{
$row = $result->fetch_assoc();
if($data == $row['gymname'])
{
echo '<option value="' . $row['gymname'] . '" selected>' . $row['gymname'] . '</option>'.PHP_EOL;
}
else
{
echo '<option value="' . $row['gymname'] . '" >' . $row['gymname'] . '</option>'.PHP_EOL;
}
}
?>
</select>
</div>
<div style="margin-bottom:10px">
My question is, how can i post the data (gymname and location) linked to the "BOOK NOW" button to the reservation.php such that the option for "gymname" and "location" will be preselected?
Thank you!
U can do <form> tag for each row or use JS witch map your data and send to the server.
It doesn't look like you have any 'names' for your inputs:
put:
name='bookNow'
in the book now button input tag.
Then put
name='gymName'
in your pre-populated gym name input tag.
Then you can get the values with php:
if(isset($_POST['bookNow'])){
$gymName = $_POST['gymName'];
}

Can not get PHP to post info to database

I've written a script to post information into my database for I can't get it to post and need help pointing out what I'm missing.
my php post for looks like this:
$query = "SELECT * from departments";
$res = mysql_query($query);
echo '<div id="department" >';
echo '<form action="depedit.php" method="post">';
echo '<input type="text" placeholder="Search departments">';
echo '<br>';
echo '</form>';
echo '</div>';
echo '<div id="depadd">';
echo '<form>';
echo '<table width="0" border="0">';
echo '<tr>';
echo '<td>Name:</td>';
echo '</tr>';
echo '<tr>';
echo '<td>';
echo "<select name='depid'>";
while ($row = mysql_fetch_array($res)) {
echo "<option value='".$row['id']."'>".$row['depname']."</option>";
}
echo "</select>";
echo '</td>';
echo ' </tr>';
echo '<tr>';
echo '<td> </td>';
echo '</tr>';
echo ' <tr>';
echo '<td><label class="limit">Select Limit for active courses in Learning Locker:</label></td>';
echo ' </tr>';
echo ' <tr>';
echo '<td>';
echo "<select name='courselimit'>";
echo "<option value='1'>1</option>";
echo "<option value='2'>2</option>";
echo "<option value='3'>3</option>";
echo "<option value='4'>4</option>";
echo "<option value='5'>5</option>";
echo "<option value='0'>Unlimited</option>";
echo "</select>";
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td> </td>';
echo '</tr>';
echo '<tr>';
echo '<td><input type="radio" id="1" name="senabled" value="1"/><label for="c1" class="required">Required<br>(Study Shredder Feature Enabled)</br></label></td>';
echo '<td><input type="radio" id="0" name="senabled" value="0"/><label for="c1" class="optional">Optional</label></td>';
echo '</tr>';
echo '<tr>';
echo '<td><input type="hidden" name="orgid" value="'.$adminorgid.'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td><input type="hidden" name="createdby" value="'.$userid.'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td><input type="hidden" name="timecreated" value="'.time(now).'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td> </td>';
echo '</tr>';
echo ' <tr>';
echo ' <td><button type="submit" class="btn">Submit</button></td>';
echo ' </tr>';
echo '</table>';
echo "</form>";
echo '</div>';
depedit.php looks like this:
$adddep = "INSERT INTO organization_dep (orgid,depid,courselimit,senabled,createdby,timecreated) VALUES ('".$_POST["orgid"].",".$_POST["depid"].",".$_POST["courselimit"].",".$_POST["senabled"].",".$_POST["createdby"].",".$_POST["timecreated"]."')";
$res = mysql_query($adddep);
if ($res === TRUE) {
echo "Department added successfully";
} else{
printf("Could not create department");
}
the correct information seems like its getting passed as the url displays the following information in it:
/index.php?depid=6&courselimit=3&senabled=1&orgid=9&createdby=1129&timecreated=1364005206
any help with this would be greatly appreciated as I'm sure its something simple that I'm just over looking.
You want to get the values from the $_POST
('".$_POST["orgid"].",".$_POST["depid"].",".$_POST["courselimit"].",".$_POST["senabled"].",".$_POST["createdby"].",".$_POST["timecreated"]."')";
but your saying it is passed through the URL, this looks like you need to use $_GET to get the values
--- and one small tip you can use echo to print multiple lines it works. as long as you close it, in the end
e.g
echo " <html>
<body>
</body>
</html>
";
I think you have a bunch of things to get right before this will be working:
Only the first form, which seems to be for searching departments, have method and action attributes
Since the second form does not have the action attribute, it is not submitting to depedit.php but to the same page that has the form.
Since the second form does not have the method attribute, it defaults to GET, and you are trying to read out POST variables in your PHP. If it was not using GET, you would not see those params in the resulting URL.
In your SQL insert statement, you must have single quotes around every single text value but not around int values. Now you have one single quote before the first value and one before the last which makes no sense.
First: this portion will not be POSTed "depedit.php" because there is no submit
echo '<form action="depedit.php" method="post">';
echo '<input type="text" placeholder="Search departments">';
echo '<br>';
echo '</form>';
Second: this will never be POSTed to depedit.php, since your <form> does not have an action specifying "depedit.php"
echo '<form>';
echo '<table width="0" border="0">';
echo '<tr>';
//other codes
echo "</form>";
maybe you mean to remove the first echo '</form>'(line 7) and the second <form>(line 10)

PHP and Session error

Hello I have a problem with a session. When I use a session to pass a variable to another page the values of that variable always still the same in the other page. No matter what row I selected. When I change the "action" to the same page where the variable is, the value shows correct. Sorry for my bad English if someone speak Spanish let my know to explain better. I really need help in this.
Here is my code:
<?php
include_once 'rnheader.php';
session_start();
$ticket_select = $_POST['serviceID'];
echo ' Create Service ';
echo '<table border="1" >';
echo '<tr>';
echo '<th>Service ID</th>';
echo '<th>Title</th>';
echo '<th>Description</th>';
echo '<th>Notes</th>';
echo '<th>Submit By</th>';
echo '<th>Assigned Employee</th>';
echo '<th>Assigned Group</th>';
echo '<th>Category</th>';
echo '<th>Status</th>';
echo '<th>Urgency</th>';
echo '<th>Customer</th>';
echo '<th>Day Created</th>';
echo '</tr>';
$query = ("SELECT ServiceID, Title, Description, Notes, SubmitBy, AssignedEmp, " .
"AssignedGroup, NameCategory, TipoStatus, TiposUrgencia, CustomerName, DayCreation FROM Service");
$result = queryMysql($query);
while ($row = mysql_fetch_assoc($result)) {
echo '<tr>';
echo '<td><form method ="post" action="rnservices1.php">';
?>
<input type="submit" name="serviceID" value=<?php echo $row['ServiceID']?>
<?php
echo '</form>';
echo '<td>'.$row['Title'].'</td>';
echo '<td>'.$row['Description'].'</td>';
echo '<td>'.$row['Notes'].'</td>';
echo '<td>'.$row['SubmitBy'].'</td>';
echo '<td>'.$row['AssignedEmp'].'</td>';
echo '<td>'.$row['AssignedGroup'].'</td>';
echo '<td>'.$row['NameCategory'].'</td>';
echo '<td>'.$row['TipoStatus'].'</td>';
echo '<td>'.$row['TiposUrgencia'].'</td>';
echo '<td>'.$row['CustomerName'].'</td>';
echo '<td>'.$row['DayCreation'].'</td>';
echo '</tr>';
}
mysqli_free_result($result);
echo $ticket_select;
$_SESSION['serviceID'] = $ticket_select;
'</table>';
?>
Is it a case issue?
$_SESSION['serviceID'] = $ticket_select;
<input type="submit" name="serviceID" value=<?php echo $row['ServiceID']?>
$ticket_select = $_POST['serviceID'];
Notice the middle one has a capital S on ServiceID and the other two are serviceID.

Categories