I have a form embedded in table that is created on the initial page load:
echo "<form id='showbooking' action='calendar.php' method='post'>";
echo "<td name='clickedcalid' value='" . $resarr[$k]['calid'];
echo "' title='" . $resarr[$k]['coms'] . "' class='hols'>";
echo $resarr[$k]['typ'] . "</td></form>";
I submit the form when a cell in the table is clicked and want to pass the value of the cell that was clicked.
<script>
$(".hols").click(function() {
$("#showbooking").submit();
})
</script>
After the reload I am testing with this:
echo "<p> ID of clicked booking was: " . $_POST['clickedcalid']; . "</p>";
but the value is not being passed and I'm not sure why.
Possibly because the form is embedded in a table?
Firstly, the form element needs to go inside the td. Secondly, only the values of form elements are sent in a form submission, ie input, select, textarea etc. Try this:
echo '<td class="hols" title="' . $resarr[$k]['coms'] . '">';
echo '<form id="showbooking" action="calendar.php" method="post">';
echo '<input type="text" name="clickedcalid" value="' . $resarr[$k]['calid'] . '" />';
echo $resarr[$k]['typ'] . '</form></td>';
You can't put <form> inside a tr element, wrapping td. <tr> specification says
Permitted content: Zero or more <td> or <th> elements, or a mix of them
Also value attribute is not valid for td element. You need to restructure your markup.
echo '<td class="hols" title="' . $resarr[$k]['coms'] . '">' .
'<form id="showbooking" action="calendar.php" method="post">' .
'<input type="hidden" name="clickedcalid" value="' . $resarr[$k]['calid'] . '">' .
$resarr[$k]['typ'] .
'</form>' .
'</td>';
To get value from $_POST you need to have input tag
echo "<form id='showbooking' action='calendar.php' method='post'>";
echo "<td><input type='text' name='clickedcalid' value='" . $resarr[$k]['calid'];
echo "' title='" . $resarr[$k]['coms'] . "' class='hols' />";
echo $resarr[$k]['typ'] . "</td></form>";
Thanks,
Naumche
Related
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.
I am creating a form to update a database and used PHP to query the database and get current values before filling them in as the values already in the input boxes. I used echo to create the HTML elements while inserting the PHP variables however, the elements created aren't being displayed. Here are a couple of the inputs that I tried to make:
// Album Price
echo "<div class=\"row horizontal-center\">" .
"<input type=\"number\" step=\"0.01\" value=\"" . htmlspecialchars($albumPrice) . "\" name=\"albumPrice\"\n" .
"</div>";
// Album Genre
echo "<div class=\"row horizontal-center\">" .
"<input type=\"text\" value=\"" . htmlspecialchars($albumGenre) . "\" name=\"albumGenre\"\n" .
"</div>";
Thanks in advance :)
Your input elements are not properly closed with a > character. This code should work:
"<input type=\"number\" step=\"0.01\" value=\"" . htmlspecialchars($albumPrice) . "\" name=\"albumPrice\" />\n"
Note the /> added in the end of the line. (the / due to <input> not having a closing tag)
There's conflicting quotes in your HTML elements. Use single quotes for echo() and double quotes for HTML elements.
Also, you did not close the <input tag.
echo '<div class=\"row horizontal-center\">' .
'<input type=\"number\" step=\"0.01\" value=\"' . htmlspecialchars($albumPrice) . '\" name=\"albumPrice\" />\n' .
'</div>';
echo '<div class=\"row horizontal-center\">' .
'<input type=\"text\" value=\"' . htmlspecialchars($albumGenre) . '\" name=\"albumGenre\" />\n' .
'</div>';
Tip: You should turn on Error Reporting by adding this code to the top of your PHP files which will assist you in finding errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
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.
Here is the code that displays the table in a form where you are to insert manually only the last field.
After filling you should go ahead and save the form into the database, but since there are no specific number of input fields (as they will vary), I need to use an array to capture all the data.
$quam = mysqli_query('select * from locstock where loccode="' . $loc . '"') or die(mysqli_error());
while ($row = mysqli_fetch_array($quam)) {
echo '<tr><td>' . getItemcode($row['itemid']) . '</td>';
echo '<td>' . getItemname($row['itemid']) . '</td>';
echo '<input type="hidden" value="' . ($row['itemid']) . '" name="itemid[0][]">';
echo '<td>' . ($row['quantity']) . '</td>';
echo '<input type="hidden" value="' . ($row['quantity']) . '" name="iquant[0][]">';
echo '<td><input type="text" title="Enter Manual Count" placeholder="Enter Manual Count" name="tally[0][]"></td>';
echo '</tr>';
}
How can I retrieve the data?
You can easily count() your INPUT fields data. as your values are part of array -> name="iquant[0][]" you can easily process them. See output of print_r($_POST); after submitting your form.
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.