How do I send all the rows in the table? - php

You are going to send the full line value using form tag. However, if you send a value, only the value in the last row is sent. How should it be modified?
in this table code
echo "<form action='../verification/medical_bills_check.php' method='post'>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td> <input type='hidden' name='id' value ='" . $row['id'] . " '>" . $row['id'] . "</td>";
echo "<td> <input type='hidden' name='storename' value ='" . $row['storename'] . " '>" . $row['storename'] . "</td>";
echo "<td> <input type='hidden' name='corporate_num' value ='" . $row['corporate_num'] . " '>" . $row['corporate_num'] . "</td>";
echo "<td> <input type='hidden' name='store_mbrno' value ='" . $row['store_mbrno'] . " '>" . $row['store_mbrno'] . "</td>";
echo "<td> <input type='hidden' name='store_mbrno_van' value ='" . $row['store_mbrno_van'] . " '>" . $row['store_mbrno_van'] . "</td>";
echo "<td ><input type='hidden' name='past_cash_receipts' value ='" . $row['past_cash_receipts'] . " '>" . $row['past_cash_receipts'] . "</td>";
echo "<td> <input type='hidden' name='past_card' value ='" . $row['past_card'] . " '>" . $row['past_card'] . "</td>";
echo "<td> <input type='hidden' name='pg_select' value ='" . $row['pg_select'] . " '>" . $row['pg_select'] . "</td>";
echo "<td> <input type='hidden' name='program_select' value ='" . $row['program_select'] . " '>" . $row['program_select'] . "</td>";
echo "<td> <input type='hidden' name='authority' value ='" . $row['authority'] . " '>" . $row['authority'] . "</td>";
echo "<td> <input type='hidden' name='apikey' value ='" . $row['apikey'] . " '>" . $row['apikey'] . "</td>";
echo "<td > <input type='submit'> </td>";
echo "</tr>";
}
echo "</form>";
and receive page code
$_SESSION["corporate_num"] = $_POST["corporate_num"];
$_SESSION["store_mbrno"] = $_POST["store_mbrno"];
$_SESSION["store_mbrno_van"] = $_POST["store_mbrno_van"];
$_SESSION["program_select"] = $_POST["program_select"];
$_SESSION["authority"] = $_POST["authority"];
$_SESSION["apikey"] =$_POST["apikey"];
I want result
id | corporate_num | store_mbrno | submit
1 1 1 submit_button
2 2 2 submit_button
3 3 3 submit_button
4 4 4 submit_button
When I submit id 1 rows.
Received Page receive value id:1 / corporate_num:1/ store_mbrno:1
When I submit id 2 rows.
Received Page receive value id:2 / corporate_num:2/ store_mbrno:2

If I understood your question correctly, the issue is that you have a lot of input fields with the same name within one form. What happens when you click one of the buttons, is that all the data in the whole form gets sent. And, because every row has fields with the same name, the values get overwritten by the next row. Therefore only the data of the last row is sent.
If you only want to send the data of one row, you need to have the data in a separate form. So, one form for each row. Since, technically, you can't have a form element directly inside a table or tr element, I suggest putting the form element and all hidden inputs in the last table cell.
Furthermore, since the contents of the hidden inputs can be modified (you can't trust your visitors not to use the inspector), you should not trust the information that is sent. Instead, if possible, only send the id, and fetch the other information anew from the database.

Related

PHP Getting Id Of Submit Button in Next Page

if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
echo "<form name=frm action=edit.php method=POST";
echo "<tr>";
echo "<td>" . $row["Sl_no"]. "</td>";
echo "<td> " . $row["nam"]. "</td>";
echo "<td> " . $row["age"]. " </td>";
echo "<td> " . $row["dob"]. "</td>";
echo "<td> " . $row["gender"]. " </td>";
echo "<td> " . $row["married"]. " </td>";
echo "<td> " . $row["temp_addr"]. " </td>";
echo "<td> " . $row["fix_addrs"]. " </td>";
echo "<td> " . $row["email"]. " </td>";
echo "<td> " . $row["phone"]. " </td>";
echo "<td> " . $row["mother_tounge"]. " </td>";
echo "<td> " . $row["nationality"]. " </td>";
echo "<td> " . $row["clg"]. " </td>";
echo "<td> " . $row["sem"]. " </td>";
echo "<td> " . $row["grad"]. " </td>";
echo "<td> " . $row["qual"]. " </td>";
echo "<td> " . $row["branch"]. " </td>";
echo "<td> <input type=submit name =" .$row["Sl_no"]. " value =Edit ><input type=button id=d".$row["Sl_no"]." value=Delete />";
echo "</tr>";
echo"</form>";
}
I am using this code
and in each field there will be a random named submit will generate.
I want to transfer the name of the button to next page or pass the value of first tag help me.
On the next page you can use $_POST['key']; or $_GET['key']. That will give you access to your value from the key.
You can not post submit button value by using name attribute. just add one hidden field with this form. like
echo "<td> <input type=hidden value=" .$row["Sl_no"]. " name=submit_btn_val >"
You need to pass the value from database to the submit button value, instead of name if you want to get the value on POST, because on post you're getting the name as key and value from input as values, like this:
<input type=submit name="name_here" value ="<?php echo $row['Sl_no']; ?>" >

Looking for assistance with passing multiple variables using POST command

I have the following code:
{
echo "<tr>";
echo "<td>" . $row1['supervisiondate'] . "</td>";
echo "<td>" . $row1['level'] . "</td>";
echo "<td>" . $row1['division'] . "</td>";
echo "<td>" . $row1['supervisor'] . "</td>";
echo "<td>" . $row1['totalscore'] . "</td>";
echo "<form action='refereesupervision.php' method='post'>";
echo "<td>
<input name='refereeID' value='" . $row['refereeID'] . "'/>
<input name='supervisionID' value='" . $row1['supervisionID'] . "'/>
<input type='submit' value='View'/>";
echo "</form></td>";
echo "</tr>";
} </br>
When refereesupervision.php receives the data using post for each of the two variables, only the refereeID is received. It should be noted that there are two separate queries of two separate tables that produce $row and $row1.
Looking for some guidance or references to help me understand.
Cheers
according to comment change input name from ID to supervisionID
<input name='ID' value='" . $row1['supervisionID'] . "'/>
<input name='supervisionID' value='" . $row1['supervisionID'] . "'/>
According to comments and editing original question. Answer is
$refereeID is not the same as $refereeid

PHP update of Query with Form

Here is code I use to update data of one row of form table. This is the code I wanna change.
The change should allows me update not only one row of the shown table but every line:
if(isset($_POST['update'])){
$UpraveneQuery = "UPDATE uctovnictvo SET meno='$_POST[meno]', datum='$_POST[datum]', obchod='$_POST[obchod]', druh='$_POST[druh]', cena='$_POST[cena]', Poznamka='$_POST[poznamka]' WHERE uct_id='$_POST[hidden]' "; //--------------
mysql_query($UpraveneQuery, $con);
};
here are data with which I work for this particular case:
$sql = "SELECT * FROM uctovnictvo WHERE meno IN ('" . implode('\', \'', $option_meno) . "') AND obchod IN ('" . implode('\', \'', $option_obchod) . "') AND druh IN ('" . implode('\', \'', $option_druh) . "') AND datum BETWEEN '$date_start' AND '$date_end' ORDER BY $order";
$mojeData = mysql_query($sql,$con) or die($sql."<br/><br/> Chyba 1 je:".mysql_error());//run query a ulozit to premennej
here is code which generates form table which allows me to change content of it. Every row of the table has its own button for deleting or updating the row.
NOW
When I clik on ulozit zmenu (update) on certain row it allows me to post values of that one row and update variables of the row (query).
PLAN
I dont want to have button ulozit zmenu (update) next to every row of the table but only one butto which will post values of every shown row of the table.
while($zaznam = mysql_fetch_array($mojeData))
{
echo "<form action=index-5.php method=post>";
echo "<tr>";
echo "<td>" . "<input type=text name='meno[]' value='" . $zaznam['meno'] . "'>" . "</td>";
echo "<td>" . "<input type=date name='datum[]' value='" . $zaznam['datum'] . "'>" . "</td>";
echo "<td>" . "<input type=text name='obchod[]' value='" . $zaznam['obchod'] . "'>" . "</td>";
echo "<td>" . "<input type=text name='druh[]' value='" . $zaznam['druh'] . "'>" . "</td>";
echo "<td>" . "<input step=any type='number[]' name=cena value='" . $zaznam['cena'] . "'>" . "</td>";
echo "<td>" . "<input type=text name='poznamka[]' value='" . $zaznam['poznamka'] . "'>" . "</td>";
echo "<td>" . "<input type=submit name='update[]' value='ulozit zmenu'" . ">" . "</td>";[]
echo "<td>" . "<input type=submit name='zmazat[]' value='zmazat'" . ">" . "</td>";
echo "</tr>";
}

Delete button for each table row

I manage to succesfully read and display data from my database with the following code:
http://pastebin.com/rjZfBWZX
I also generate a delete button for each row of the table :) Clicking the delete button calls "obrisi.php" which is supposed to delete that row but I messed something up :S Here's obrisi.php code:
http://pastebin.com/mrFy1i7S
I'm getting a Parse error: syntax error, unexpected 'FROM' (T_STRING) :S
Let's try and do it with _GET instead of _POST.
In your main code you need to change line 39 (the input) from:
echo "<td>" . " <input type='submit' id= '$id' . ' value='Delete' >" . "</td>";
to:
echo "<td><a href='obrisi.php?id=$id'>Delete</a></td>";
In your obrisi.php change line 3 (the id setup) from:
$id = $_POST['id'];
to:
$id = $_GET['id'];
And finally as a nice addition, redirect back to the main page by adding the following line at the end of the obrisi.php file before the closing of the php tag.
header('location:index.php');
Where index.php the name of the main page you have.
echo "<td>" . " <input type='submit' id= '$id' . ' value='Delete' >" . "</td>";
some simple errors here. this would output (with $id = 1):
<td><input type='submit' id= '1' . ' value='Delete' ></td>
this line should be corrected to
echo '<td><input type="submit" id="' . $id . '" value="Delete" ></td>';
this is also going wrong.
echo "<form action="obrisi.php" method="post">";
should be like:
echo '<form action="obrisi.php" method="post">';
But the main problem is that there is no field id given in the post. The id of a html element is not sent on submit. it is basically to identify that element in the HTML structure.
And when using a submit button you will have to limit the scope of the form to that row and use a hidden input field, or use a link like thanpa suggests
to clarify: if you want to do it with a post (but i would sugget using the $_GET)
while ($row = mysqli_fetch_array($result) )
{
$id = $row['id'];
echo "<tr>";
echo '<form action="obrisi.php" method="post">';
echo "<td>" . $row['Ime'] . "</td>";
echo "<td>" . $row['Prezime'] . "</td>";
echo "<td>" . $row['Grad'] . "</td>";
echo "<td>" . $row['Drzava'] . "</td>";
echo "<td>" . $row['Obavijesti'] . "</td>";
echo "<td>" . $row['Tekst'] . "</td>";
echo "<td>"
echo '<td><input type="hidden" name="id" value="' . $id . '"/><input type="submit" value="Delete" ></td>';
echo "</form>
echo "</tr>";
}
and remove the echo's for the form from start and end of script.
as an additional note here, if this is going to be at some point being used in a live system you need to be checking $id in obrisi.php that it is actually an ID and not something nasty and unexpected like more sql, look up sql injection.

Dynamic naming of radio buttons

I have "n" number of rows being returned by a sql statement. Works great. For every row returned, I would like the user to pick an option (either option 'a' or option 'b')
As standard practice a while loop is used to iterate through each row, and I attempt to name my options by the row number. However, I run into problems when I attempt to get the value of the radio button the user has selected.
I can't seem to dynamically get the value of the radio button named 1 in the first loop (or any loop with it's appropriate name) of the where clause.
Code snippet here:
$counter = 1;
while($row = mysql_fetch_array($result))
{
echo "<form action='updatepics.php' method='post'>";
echo "<input type='radio' name='m" . $counter . "' value= ". $row['f_id'] ."> " . $row['Favorite'];
echo "<input type='hidden' name='game' value=" . $counter . ">";
echo " vs " . $row['Underdog'] ." ";
echo "<input type='radio' name='m" . $counter . "' value= ". $row['d_id'] ."> ";
echo "<br>";
echo "<input type='hidden' name='choice' value=" . $_GET["m" . $counter] . " >";
echo "<input type='submit' value='Make Choice'>";
echo "</form>";
$counter ++;
}
updatepics.php:
Game
<?php
echo $_POST['game'];
echo " " . $_POST['player'] . " picks: ";
echo " " . $_POST['choice'] ;
?>
output of updatepics.php...
Game 1 granny picks:
Notice 'choice' is null
According to the HTML4 specification, names attributes are defined as CDATA and CDATA is defined as follows:
ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").
Modify your code to have all name values start with at least one letter.
I bailed out from dynamically creating a radio button name. Now all radio buttons have the same name through each iteration of the loop.
It seems to work passing the correct value at the correct time even if you select all of the buttons are set before hitting the submit button for each iteration.
echo "<form action='updatepics.php' method='post'>";
echo "<input type='radio' name='mw' value= ". $row['f_id'] ."> " . $row['Favorite'];
echo "<input type='hidden' name='game' value=" . $row['f_id'] . ">";
echo "<input type='hidden' name='player' value='" . $user . "' >";
echo " vs " . $row['Underdog'] ." ";
echo "<input type='radio' name='mw' value= ". $row['d_id'] ."> ";
echo " " . $row['line'];
echo "<input type='submit' value='Make Choice'>";
echo "</form>";
updatepics.php is just as simple, and it works:
$_POST['mw']

Categories