Looping through $_POST Variable - php

I have a PHP script that generates some form on the page I have. I also have a table and submit button within the form which all works fine and the data is sent to the next page as intended. Now on the next page I have a PHP code that reads the $_POST variable. Here is my code:
echo "<form name=\"myForm\" id=\"myForm\" method=\"post\" action=\"\">";
echo "<table>";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>Name</th>";
echo "<th>Group</th>";
echo "</tr>";
while($row = mysql_fetch_array($qryRes))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td><input name=\"aName" . $row['id'] . "\" id=\"aName" . $row['id'] . "\" type=\"text\" value=\"" . $row['aName'] . "\" /></td>";
echo "<td><select name=\"gID" . $row['id'] . "\">";
while($gRow = mysql_fetch_array($grpsQry))
{
if($gRow['id'] == $row['groupID'])
echo "<option id=\"" . $gRow['id'] . "\" selected=\"selected\">" . $gRow['gName'] . "</option>";
else
echo "<option id=\"" . $gRow['id'] . "\">" . $gRow['gName'] . "</option>";
}
echo "</select></td>";
echo "</tr>";
mysql_data_seek( $grpsQry, 0 );
}
echo "</table>";
echo "<input type=\"submit\" name=\"submitForm\" value=\"Save\" />";
echo "</form>";
Then comes my PHP Code to read the $_POST:
if(isset($_POST['submitForm']))
{
print_r($_POST);
foreach($_POST as $x)
echo $x . "<br />";
}
Everything works fine, except that I need to read only the text field in the foreach loop as I will generate a query on them and will compare the values I will have with the value returned from the List ..
As yuo can see, the textfield is named aName then the id of the user. I need to read this only. But I will use it to generate a query which will be then compared to the value of the List gID.

try this:
foreach($_POST as $key => $value){
if(substr($key,0,5) == 'aName')
echo $value . "<br />";
}

Related

On submit form Display data into table using echo in PHP

I have a table, displayied with an echo, where, for each row I'd like to have a button to change a "status" field (I want to update just one field). I am trying to insert a form button into my echo but I don't get anything.
This is my echo code:
while($row = mysql_fetch_array( $result )) {
echo "<tr>";
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['fullname'] . '</td>';
echo '<td>' . $row['message'] . '</td>';
echo '<td>' . $row['country'] . '</td>';
echo '<td>' . $row['email'] . '</td>';
echo '<td>' . $row['website'] . '</td>';
echo '<td>' . $row['date'] . '</td>';
echo "<td>" . $row['status'] . " </td>";
echo "<form action='updatestatus.php' method='post'>";
echo "<input type='hidden' value='".$row['id']."' name='id' />";
echo "<input type='hidden' value='".$row['status']."' name='status' />";
echo "<td><input type='submit' name='submit' value='Submit'></td>";
echo "</form>";
echo '<td>Edit</td>';
echo '<td>Delete</td>';
echo "</tr>";
}
updatestatus.php
// check if the form has been submitted.
if (isset($_POST['submit'])) {
// confirm that the 'id' value is a valid integer before getting the form data
if (is_numeric($_POST['id'])) {
// get form data, making sure it is valid
$id = $_POST['id'];
$status = $_POST['status'];
if ($status == 0) {
$status == 1;
mysql_query("UPDATE personalguestbook SET status='$status' WHERE id='$id'")
or die(mysql_error());
} else {
$status == 0;
mysql_query("UPDATE personalguestbook SET status='$status' WHERE id='$id'")
or die(mysql_error());
}
header("Location: view.php");
}
}
So, if my status field is = 0, I want to change it = 1 and update into my database, and vice versa.
Thanks for your attention
Your form isn't sending anything to updatestatus.php, you can add the data to POST in a hidden form like so:
echo "<form action='updatestatus.php' method='post'>";
echo "<input type='hidden' value='".$row['id']."' name='id' />";
echo "<input type='hidden' value='".$row['status']."' name='status' />";
echo "<td><input type='submit' name='submit' value='Submit'></td>";
echo "</form>";
This is the simple fix to your code above, there are still some security flaws and efficiency methods others have mentioned in the comments.
Check this PHP code
while($row = mysql_fetch_array( $result )) {
echo "<tr>";
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['fullname'] . '</td>';
echo '<td>' . $row['message'] . '</td>';
echo '<td>' . $row['country'] . '</td>';
echo '<td>' . $row['email'] . '</td>';
echo '<td>' . $row['website'] . '</td>';
echo '<td>' . $row['date'] . '</td>';
echo "<td>" . $row['status'] . " </td>";
echo "<td>";
echo "<form action='updatestatus.php' method='post'>";
echo "<input type='hidden' value='".$row['id']."' name='id' />";
echo "<input type='hidden' value='".$row['status']."' name='status' />";
echo "<td><input type='submit' name='submit' value='Submit'></td>";
echo "</form>";
echo "</td>";
echo '<td>Edit</td>';
echo '<td>Delete</td>';
echo "</tr>";
}
updatestatus.php
// check if the form has been submitted.
if (isset($_POST['submit']))
{
// confirm that the 'id' value is a valid integer before getting the form data
if (is_numeric($_POST['id']))
{
// get form data, making sure it is valid
$id = $_POST['id'];
$status = $_POST['status'];
if ($status == 0){
$status == 1;
mysql_query("UPDATE personalguestbook SET status='".$status."' WHERE id='".$id."'")
or die(mysql_error());
} else {
$status == 0;
mysql_query("UPDATE personalguestbook SET status='".$status."' WHERE id='$id'")
or die(mysql_error());
}
header("Location: view.php");
}
}

Assigning ID's to fields that are echoed using PHP inside a while loop

I currently have two dropdown menus that a user will select the course and a golfer which will load a scorecard.
What I am trying to do now is when a user enters a value into the "Score" field I want the "points" to be automatically shown ("Points" is a read only input field). To do this I am assuming that I will have to give each table row for score and points a specific ID.
echo "<div class='scorecardTable'>
<table 'id=scorecardTable'>
<tr>
<th>HoleNumber</th>
<th>Par</th>
<th>Stroke Index</th>
<th>Score</th>
<th>Points</th>
</tr>'";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['holeNumber'] . "</td>";
echo "<td>" . $row['par'] . "</td>";
echo "<td>" . $row['strokeIndex'] . "</td>";
echo "<td> <input type='text' maxlength='2' id='score' /></td>";
echo "<td> <input type='text' id='points' readonly></td>";
echo "</tr>";
}
echo "</table>";
From the above code I am using PHP to print the information from my database but I was just wondering if anyone would know how to assign a unique ID to each of the score and points input fields so I can apply the calculation to each user input.
You could use a variable to count the rows and set the id.
$rowIndex = 0;
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['holeNumber'] . "</td>";
echo "<td>" . $row['par'] . "</td>";
echo "<td>" . $row['strokeIndex'] . "</td>";
echo "<td> <input type='text' maxlength='2' id='score$rowIndex' /></td>";
echo "<td> <input type='text' id='points$rowIndex' readonly></td>";
echo "</tr>";
$rowIndex++;
}
You may also use
id='score[$rowIndex]'

How to display an option from a selectbox?

I'm trying to create a table that shows the time and the task a user has to do.
All this is saved in a database. For each row and column there is a selectbox(Usernames) and an <input>(for comments).
Here is a screenshot: http://snag.gy/NYwsJ.jpg
The name of each selectbox is the id of the time and task
echo "<select name='" . $x['idTask'] . "-" . $y['idTime'] . "'>
(e.g: name="1_23")
The problem is when I load the page it is supposed to display all the records that are already saved in the database.
For the <select> I tried using this an if:
if ($x["fiUser"] == $z["idUser"]){echo "selected";}
And here is the table:
<?php
echo"<div class='dv_Table'><table><tr><th></th>";
foreach (SelectTime() as $r) {
echo "<th>" . $r['dtTime'] . "</th>";
}
echo "</tr>";
echo "<form method='post'>";
$userselect = "";
foreach (SelectCalTask() as $x) {
echo "<tr><td>" . $x['dtTask'] . "</td>";
foreach (SelectTime() as $y) {
echo "<td>";
echo "<select name='" . $x['idTask'] . "-" . $y['idTime'] . "'>
<option value='0'> -- None -- </option> ";
foreach (SelectUser_Name() as $z) {
echo "<option value='" . $z["idUser"] . "'";
if ($x["fiUser"] == $z["idUser"]) {
echo "selected";
} echo" >" . $z["dtFirstName"] . " " . $z["dtLastName"] . "</option> ";
}
echo " </select>
<input type='text' name='" . $x['idTask'] . "_" . $y['idTime'] . "' value='" . "_" . $y['idTime'] . "'></td>";
}
echo "</tr>";
}
echo "</table></div>";
echo "<input type='submit' name='updateCalendar' value='Update'></form>";
?>
Better, would be if could send an Update query right after the "onchange" or right after finishing the comment in the input.
Is that possible ?
Thank you in advance
*
Better, would be if could send an Update query right after the
"onchange" or right after finishing the comment in the input. Is that
possible ?
*
Why not?
$('.yourValue').on('change', function (e) {
$.ajax({
type: "POST",
url: "/path-to-update-query",
success: function (data) {
}
});
});

how to use a link to download a file in php

I am using the following code to fill the drop-down box and select a file and download it. Its working perfect.But i tried using every file to display as link and download it on clicking the link.
echo "<form id=\"form\" name=\"psform\" action=\"download_logic.php\" method=\"post\"><label>Select File:&nbsp</label><select name=\"file\" >";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['location'] . "'>" . $row['location'] . "</option>";
}
echo "</select></label>";
echo"<br>";
echo "<input id=\"submit\" type=\"submit\" name=\"filesubmit\" value=\"Download\" /> </form>";
its giving me errors ....any help please....
I am using following code:
while ($row = mysql_fetch_array($result)) {
echo "" . $row['fileshare'] . "";
}
Here is the error in the <a> tag*** Error (unexpected T_ENCAPSED_AND_WHITESPACE)*
this should work:
echo '' . $row['fileshare'] . '';
I'm really sorry I can't tell you why it happens, but if you change all of the \" to ' it should work for you.
echo "<form id='form' name='psform' action='download_logic.php' method='post'><label>Select File:&nbsp</label><select name='file' >";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['location'] . "'>" . $row['location'] . "</option>";
}
echo "</select></label>";
echo"<br>";
echo "<input id='submit' type='submit' name='filesubmit' value='Download' /> </form>";
As for the next part, you can't add a variable to a string just by writing
"variable: . $take['variable']"
If you wish to add variables to string close the writen part of the string and then add the variable, like this:
"variable: ". $take['variable']
Here is how it sould look like:
while ($row = mysql_fetch_array($result)) {
echo "<a href='download_logic.php?f=". $row['location'] . $row['fileshare'] ."'>" . $row['fileshare'] . "</a>";
}

Selecting values from the database and make the checkbox checked if it does exist

//query to check if part id number exists in table ATTEND where service id = ...
$result2 = mysql_query("SELECT * FROM attend WHERE SIDno='$SIDno' and ServiceID='$id");
//if exists $ok = true;
if (mysql_num_rows($result2)>0) {
$ok == true;
}
echo "<tr bgcolor=$bgcolor>";
echo "<td><a name=$row1[0] id=$row1[0]>$row1[0]</td>";
echo "<td>" . $row1[1] . "</td>";
echo "<td>" . $row1[5] . "</td>";
echo "<td>" . $row1[2] . "</td>";
echo "<td>" . $row1[3] . "</td>";
echo "<td><input type='checkbox' name='checkbox[]' value=" . $row1[0];
if ($ok == true) {
echo 'disabled="disabled" checked="checked"';
}
echo "></td>";
echo "<input type='hidden' name='ServiceID' value=" . $id . ">";
echo "<input type='hidden' name='Year' value=" . $Year . ">";
echo "<input type='hidden' name='Stype' value='Recollection'>";
echo "</tr>";
}
}
echo "<tr>
<td colspan='5' align='right' bgcolor='#FFFFFF'><input name='SUBMIT1' type='submit' id='SUBMIT'value='SUBMIT'></td>
</tr>";
How can implement that on the next load if the value of the checkbox is already available in the database it will now be checked. but if it is not yet existing i can check it and save it to the database.
I'm guessing the problem lies in this line:
if (mysql_num_rows($result2)>0) {
$ok == true;
}
It should be:
if (mysql_num_rows($result2)>0) {
$ok = true;
}
In the first snippet you are just testing if $ok is equal to true, while in the second example an actual assignment to the variable is performed.
Remember:
= != ==

Categories