PHP $_POST in form action not working - php

there is something wrong with the php post method, using php, the form code is the following:
<form method="post" action="keres.php">
<select name="kategoria_keres" id="kategoria">
<?php
$kategoria = mysqli_query($con,"SELECT distinct mufaj FROM adatok");
while ($row = mysqli_fetch_array($kategoria)) {
echo '<option value="' . $row['id'] . '">' . $row['mufaj'] . '</option>' ;
}
?>
</select>
<input type="submit" value="Keresés" id="keres" name="keres" />
</form>
The aim is to give the selected option's value using php $_POST method.
the processing php is the following:
<?php
include ("functions.php");
include ("connect.php");
$mufaj = $_POST['kategoria_keres'];
$result = mysqli_query($con,"SELECT * FROM adatok where mufaj='$mufaj'");
while ($row = mysqli_fetch_array($result)) {
echo '<tr>';
echo '<td>' . '' . $row ['cim'] . '' . '</td>';
echo '<td>' . $row['orszag'] . '</td>';
echo '<td>' . $row ['mufaj'] . '</td>';
echo '</tr>';
}
?>
When trying to print_r the $_POST it is empty, so this is the reason why the sql query returns with empty output.
The question is why does the $_POST not working, when the form passing it to the processing php?

please make sure your processing php file name is ="keres.php" because in the form action you set the keres.php.

Related

Get request doesnt work

I have a form that queries my server for data using MySQL. I am using a form that sends get requests. It doesn't show anything and I don't know why. I is so strange because my query is valid and I tested it on PHPmyadmin..I am not striving for answers only, I want to know why this happened and what is the reason behind it.
Here is my code:
<form name="get" action="Roster.php" method="get">
<select name="course" id="course">
<?php
$get = mysqli_query($con, "SELECT teaching.Course_ID FROM `teaching` WHERE teaching.F_ID=213000000 ");
while ($row = mysqli_fetch_assoc($get)) {
echo '<option value ="' . $row["Course_ID"] . '"> ' . $row["Course_ID"] . ' </option>';
}
?>
</select>
<select name="group">
<?php
$get = mysqli_query($con, "SELECT `Group_ID` FROM `teaching` WHERE `teaching`.F_ID= 213000000");
while ($row = mysqli_fetch_array($get)) {
echo '<option value ="' . $row["Group_ID"] . '"> ' . $row["Group_ID"] . ' </option>';
}
?>
</select>
<date-util format="yyyy-mm-dd">
<label for="Date" > Date </label><input id="meeting" name="date" type="date" />
</date-util>
<input type="submit" name="Send" value="Get"/>
</form>
<?php
if ($_GET['submit']) {
$sql = " SELECT enrollment.S_ID,student.ID,student.F_Name,student.L_name,attendance.Status,attendance.Date
From enrollment
INNER JOIN student On enrollment.S_ID
INNER JOIN attendance On enrollment.S_ID
where enrollment.Course_ID =" . $_GET["course"] . "and enrollment.Group_ID =" . $_GET["group"] . "and attendance.date =" . $_GET["date"] . " ";
$result = mysqli_query($con, $sql);
$message = "Please Choose Course_ID and Group_ID ";
if ($result > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo "Hello";
echo "<tr>";
echo '<td>' . $row['ID'] . '</td>';
echo '<td>' . $row['F_Name'] . " " . $row['L.name'] . '</td>';
echo '<td>' . $row['Date'] . '</td>';
echo '<td>' . $row['Status'] . '</td>';
echo "</tr>";
}
} else {
echo "<tr>";
echo '<td>' . $message . '</td>';
}
}
?>
$_GET['submit'] does not exist. You need to add submit as your name attribute to your button like so
<input type="submit" name="submit" value="Get"/>
Also you should use prepared statements to prevent SQL injection attacks.

PDO update row value in foreach loop

I created a table, inside a foreach loop displaying in the table all the records. Then I included a SQL update with the select and update one array value. When I select different value, the loop updates all the records in the table and NOT only one selected to be updated. I was all day struggling with it, please help me.
If foreach is a wrong way, I would highly appreciate your suggestions of how to display a table that would display records and permit updates for each array.
<?php
foreach ($stmt as $key => $row)
{
$newcustomerid = htmlentities($row['customer_id']);
echo '<tr><td>' . htmlentities($row['customer_company']) . '</td>';
echo '<td> id:' . $newcustomerid . 'key: ' . $key .'</td>';
echo '<td>' . htmlentities($row['customer_email']) . '</td>';
echo '<td>' . htmlentities($row['customer_phone']) . '</td>';
echo '<td>' . htmlentities($row['customer_country']) . '</td>';
echo '<td>' . htmlentities($row['customer_city']) . '</td>';
echo '<td>' . htmlentities($row['customer_segment']) . '</td>';
echo '<td>' . htmlentities($row['customer_updated']) . '</td>';
$customer_status = htmlentities($row['customer_status']);
if(isset($_POST['Submitbb'])){ //check if form was submitted
$input = $_POST['Submitbb']; //get input text
$stmtUpdateStatus = $conn->prepare("UPDATE user_customers SET `customer_status` = :customer_status WHERE `customer_id` = :customer_id");
$stmtUpdateStatus->execute(array(':customer_status' => $input, ':customer_id' => $row['customer_id']));
}
echo '<td>
<form action="" method="post">
<select name="Submitbb" onchange="this.form.submit();">
<option> - ' . $customer_status . ' - </option>
<option>Susisiekti</option>
<option>Priminimas 1</option>
<option>Priminimas 2</option>
<option>Paskambinti</option>
<option>Netinkamas klientas</option>
</select>
</form>
</td>';
echo '<td>' . 'Išsaugoti' . '</td></tr>';
}
?>
Thank you.
You could use a hidden input to capture the row in the database you wish to update
echo '<td>
<form action="" method="post">
<input name="customer_id" type="hidden" value="'.$newcustomerid.'"/> <!-- hidden -->
<select name="Submitbb" onchange="this.form.submit();">
<option> - ' . $customer_status . ' - </option>
<option>Susisiekti</option>
<option>Priminimas 1</option>
<option>Priminimas 2</option>
<option>Paskambinti</option>
<option>Netinkamas klientas</option>
</select>
</form>
</td>';
Then compare it when the form is posted
if(isset($_POST['Submitbb'])){ //check if form was submitted
$input = $_POST['Submitbb']; //get input text
$customer_id = $_POST['customer_id']; // get the posted customer ID
if($row['customer_id'] == $customer_id){
$stmtUpdateStatus = $conn->prepare("UPDATE user_customers SET `customer_status` = :customer_status WHERE `customer_id` = :customer_id");
$stmtUpdateStatus->execute(array(':customer_status' => $input, ':customer_id' => $customer_id));
}
}

PHP-HTML5-Form - My query-generated dropdown posts a blank value

When I load my page, the value of the variable, $v_UpdateONE, is "Select Version". When I select a version, the value goes blank.
I need to grab the selected value for use in a DB update statement.
Thank you for any assistance. -James
<FORM METHOD="post" ACTION="Update.php" WIDTH="50">
<?php
$avQuery = "SELECT $v_software1 FROM version_master.vermas_availableversions WHERE $v_software1 IS NOT NULL ORDER BY SortCol DESC";
$a_AvailVers = mysql_query($avQuery);
#_Version dropdown box
echo "<select NAME='AvailVersONE' ONCHANGE=submit()>";
echo "<option>Select Version</option>";
while ($row = mysql_fetch_array($a_AvailVers)) {
echo "<option value='" . $row['$v_software1'] . "'>" . $row[$v_software1] . "</option>";
}
echo "</select>";
$v_UpdateONE = $_POST['AvailVersONE'];
echo $v_UpdateONE;
?>
</FORM>
You have an error in
value='" . $row['$v_software1'] . "'
Since $v_software1 is in single quotes, it will be literal $v_software1.
Try removing the quotes -
value='" . $row[$v_software1] . "'
You need to post before you can read $_POST data.
Form File
<FORM METHOD="post" ACTION="Update.php" WIDTH="50">
<?php
$avQuery = "SELECT $v_software1 FROM version_master.vermas_availableversions WHERE $v_software1 IS NOT NULL ORDER BY SortCol DESC";
$a_AvailVers = mysql_query($avQuery);
#_Version dropdown box
echo "<select NAME='AvailVersONE' id='AvailVersONE' ONCHANGE=submit()>";
echo "<option>Select Version</option>";
while ($row = mysql_fetch_array($a_AvailVers)) {
echo "<option value='" . $row['$v_software1'] . "'>" . $row[$v_software1] . "</option>";
}
echo "</select>";
?>
<button type="submit"> <!-- this will draw a submit button -->
</FORM>
then on your Update.php
<?php
$v_UpdateONE = $_POST['AvailVersONE'];
echo $v_UpdateONE;
?>
Sometimes, the ID needs to be filled up (browser dependent)

Why does the my select form not send the variable in the value parameter?

I am making a page that sends data in the POST and refreshes when you click on a select list option. The data is retrieved from a database. I am using the this.form.submit() function to send the variable when you click on an option. However, for some reason, it doesn't send the variable in the value="" of the option box but the text in between the ><. Below is the piece of code I thought was relevant:
echo '<form method="post">';
echo "<select onchange='this.form.submit()' id='chose_category' name = 'chose_category'>";
while($row = sqlsrv_fetch_array($hoofdrubrieken)){
echo '<option "value="' . $row['number'] . '">' . $row['name'] . '</option>';
}
echo '</select>';
echo'</form>';
In this case, the variable in $row['name'] is send as a POST variable, instead of $row['number']. I have checked this by printing the POST variable. Is there any way to send $row['number'] here, but still displaying $row['name']?
Change
while($row = sqlsrv_fetch_array($hoofdrubrieken)){
echo '<option "value="' . $row['number'] . '">' . $row['name'] . '</option>';
}
To:
while($row = sqlsrv_fetch_array($hoofdrubrieken)){
echo '<option value="' . $row['number'] . '">' . $row['name'] . '</option>';
}
Your current produces something like :
<select onchange='this.form.submit()' id='chose_category' name = 'chose_category'>
<option "value="number">name</option>
</select>
You need to remove the double-quote.

From where do i pass id to delete a record?

How do i pass id to delete record in this code?
<form action="index.php">
<?php
mysql_connect('localhost', 'root', '');
mysql_select_db('user');
$query = mysql_query("Select * from tbluser");
echo "<center>";
echo '<table style="border:solid 2px black;">';
while(($row = mysql_fetch_array($query)) != NULL) {
echo '<tr>';
echo '<td>' . $row['UserName'] . '</td>';
echo '<td>' . $row['Password'] . '</td>';
echo '<td>' . $row['EmailAddress'] . '</td>';
echo '<td>' . $row['Address'] . '</td>';
echo '<td>' . $row['Address'] . '</td>';
echo '<td><input type = "Submit" value = "Delete" name = "btnDel" /></td>';
echo '</tr>';
}
echo '</table>';
echo "</center>";
?>
</form>
The above code is in index.php and it is submitting it to itself.
Without needing javascript, seperate GET urls etc, just plain old HTML & the original POST: just add the ID to the name of the button:
<input type="submit" value="Delete" name="btnDel[<?php echo $id;?>]">
And in receiving code:
if(isset($_POST['btnDel']) && is_array($_POST['btnDel'])){
foreach($_POST['btnDel'] as $id_to_delete => $useless_value){
//delete item with $id_to_delete
}
}
Here's how I would do it:
Change
echo '<td><input type = "Submit" value = "Delete" name = "btnDel" /></td>';
To
echo '<td><input type = "button" value = "Delete" onclick = "btnDel(' . $row['id'] . ')" /></td>';
Add the following field to the form (outside of the while loop of course):
<input type="hidden" name="id" id="userid" />
Define the following javascript function:
function btnDel(id) {
if (confirm("Really delete id=" + id + "?")) {
document.getElementById('userid').value = id;
document.forms[0].submit();
}
}​
Then you can retrieve that value using $_GET['id'] or $_POST['id'] depending on your form's method.
EDIT: Here's a working demo, and its source
Use this to submit the id as a part of form.
<input type="hidden" id="id" name="id" value="<? echo $row['id']; ?>" />
or you can send values in URL to do the same thing
An example:
Delete
A full working sample
<?
mysql_connect('localhost', 'root', '');
mysql_select_db('user');
switch($_GET['action']) {
case "delete":
$query = "DELETE FROM tbluser WHERE id='".$_GET['id']."'"
$result = mysql_query($query);
break;
//well other actions
}
$query = mysql_query("Select * from tbluser");
echo "<center>";
echo '<table style="border:solid 2px black;">';
while(($row = mysql_fetch_array($query)) != NULL) {
echo '<tr>';
echo '<td>' . $row['UserName'] . '</td>';
echo '<td>' . $row['Password'] . '</td>';
echo '<td>' . $row['EmailAddress'] . '</td>';
echo '<td>' . $row['Address'] . '</td>';
echo '<td>' . $row['Address'] . '</td>';
echo '<td>Delete</td>';
echo '</tr>';
}
echo '</table>';
echo "</center>";
?>
You could also send it in the url at action="index.php" given that you move it below while(($row = mysql_fetch_array($query)) != NULL) { like this
echo "<form action='index.php?var=" . $row['id'] . "'>";
You would then get the variable using $_GET['var']
If the id needs to stay hidden (on the page and in the link) a hidden input element and submitting the form using method="post" like previously suggested would be the better way to go.
From the table:
echo '<input type="hidden" name="user_id" value="'.$row['id'].'" />';
echo '<td><input type = "Submit" value = "Delete" name = "btnDel" /></td>';
This will be sent to the server as a parameter named user_id. Ideally, you should be using the POST method in the form.
This assumes that the user id is named, well id in the table.
BTW, the != NULL is unnecessary.
It's sufficient to write:
while($row = mysql_fetch_array($query)) {
NULL evaluates to FALSE in a boolean context.
Update:
As mentioned in comments to another answer, there is an issue with your approach. With multiple rows, you won't be able to distinguish between user ids.
One solution would be use multiple forms.
Another option would be to have the name of the submit button include the id. You'd then parse this out of the name of the $_POST array keys.

Categories