I'm generating a html table. Each row starts with a Edit-Button.
Site A:
echo "<table border='1' align='center'>";
echo "<tr>"
. "<th style='font-weight:bold'></th>"
. "<th>".$nr.":</th>"
. "<th>Anschrift</th><th>Nachname</th>"
. "<th>Vorname</th>"
. " <th>PLZ</th>"
. "<th>Ort</th>"
. "</tr>";
echo "<form action='lxKundenEdit.php' method='POST'>";
$i=0;
foreach($arr as $key =>$value)
{
echo "<tr><td><input type='submit' name='btn'.$i.'\'' value='Bearbeiten'/></td>";
foreach($value as $subkey=>$subValue)
{
echo "<td>".$subValue."</td>";
}
echo "</tr>";
$i++;
}
echo "</form>";
echo "</table>";
Then I want to know which button has been pushed. When vardump the POST-Array it seems nothing really works. Any hints about this? regards, Ismir
Site B:
var_dump($_POST['btn0']); //f.e.
Simply pass the sender ID in a hidden input
<input type="hidden" name="sender" value="<?= $senderID; ?>">
On Site B you can then get the value from the $_POST variable:
echo $_POST['sender'];
Edit
I see I misread your question. What you can do is pass your subkey on the submit button as follows:
<input type="submit" name="submit[<?= $subkey; ?>]" value="send" />
You should give each button a unique value. When the form is posted you can then check for that value for that button.
echo '<input type="submit" name="btn'.$i.'" value="'.$i.'" />';
Edit:
As Peter pointed out in the comments, if you want to change the text of your button, you can use the button element:
<?php
echo '<form method="post">';
echo '<button type="submit" value="button 1 was used" name="button">Send</button>';
echo '<button type="submit" value="button 2 was used" name="button">Send</button>';
echo '</form>';
var_dump( $_POST[ 'button' ] );
All you need to do is:
Make array of all your submit buttons with ids as keys.
echo '<tr><td><input type="submit" name="btn[<?php echo $i;?>]" value="Bearbeiten"/></td>';
And then in PHP, get posted submit buttons like:
if (! empty($_POST['btn']) {
foreach ($_POST['btn'] as $btnId => $btnVal) {
// $btnId is Id of the button that is $i
// $btnId means button is pressed.
// $btnVal is value of the button
}
}
Just include a hidden input inside your form, eg
<input type='hidden' name='submit'/>
So you can use it to check, if the form has been sent (if(isset($_POST['submit'])) etc). Then check any other $_POST fields looking for any ['btn...'], eg
foreach($_POST as $key => $value){ // check all the $_POST fields
if(substr($key,0,3) == 'btn'){ // if its name begins with 'btn'...
$button_id = substr($key,3,NULL); // then that's some button. Get its ID
}
}
Think this should work
Related
I have PHP code creating multiple forms on a single page and the submission of any of these forms should trigger an API call based on which form was submitted.
if(isset($_POST['pickApples'])) {
echo '<h1>Picking Apple ' . $_POST['appleId'] . '</h1>';
// Function that calls API
pickApple($_POST['appleId']);
}
// $listOfApples is a list of 10 apples each with a unique ID
foreach ($listOfApples as $apple) {
echo '<form method="post" action="mypage.php">';
echo '<input type="hidden" name="appleId" value="' . $apple->{"id"} . '">';
echo '<button type="submit" class="my-button-class" value="click" name="pickApples">Pick Me</button>';
echo '</form>';
}
No matter which of the form buttons I click, the value in $_POST['appleId'] is the ID of the first apple in the list. I don't have much experience with PHP or HTML forms, is my approach completely off?
I am seeing some small mistakes as
echo '<input type="hidden" name="appleId" value"' . $apple->{"id"} . '">';
You should fix it,instead of foreach yor can use for loop to display forms,
as I haven't $listofapples,I used 10,you can query for row numbers than place row numbers value as $listofapples,then echo your unique id to input value,
Finally it's working as you wished
Check it & let me know if it's the perfect fit for you
<?php
if(isset($_POST['pickApples'])) {
echo '<h1>Picking Apple ' . $_POST['appleId'] . '</h1>';
// Function that calls API
pickApple($_POST['appleId']);
}
$listOfApples = 10;
// $listOfApples is a list of 10 apples each with a unique ID
$sn=0;
for ($i = 1; $i <= $listOfApples; $i++) {
echo '<form method="post" action="hy.php">';
echo '<input type="hidden" name="appleId" value=" ' .++$sn. '"/>';
echo '<button type="submit" class="my-button-class" value="click" name="pickApples">Pick Me</button>';
echo '</form>';
}
?>
I'm using php to dynamically create multiple buttons on my web page that have increasing values 01 to 09. When I create the forms as below the buttons are created as expected with the correct values but when I submit the form an empty array is posted.
The code here is inside a for loop that increments $i:
echo '<form action="test.php" method="post">';
echo '<input type="hidden" name="RunSmokeTest" value="0'.$i.'">';
echo '<input type="submit" name="submit" value="Run Test">';
echo '</form>';
Here is the HTML that is created:
However, when I change the code to take out the $i variable, the form will be submitted as expected but all the buttons will have the same value which I can't have.
Here is the start of test.php that prints out array(0) { } when I click the submit button and an alert comes up with message 'empty'.
var_dump($_POST);
if(empty($_POST['RunSmokeTest']))
{
echo '<script language="javascript">';
echo 'alert("empty")';
echo '</script>';
}
else{
...
}
Try this .
<?php
if(isset($_POST['submit'])) {
echo $_POST['RunSmokeTest'];
}
for ($i=0; $i < 10; $i++) {
echo '<form action="test.php" method="post">';
echo '<input type="hidden" name="RunSmokeTest" value="0'.$i.'">';
echo '<input type="submit" name="submit" value="Run Test">';
echo '</form>';
}
?>
Demo is here
I would like some help printing what is in a certain textbox that was created by an echo command.
while($row = $result->fetch_assoc()){
$stringTest = $row['Price'];
$AssetId = $row['AssetId'];
echo "<center><div> <h3>Cost: ".$stringTest."";
echo '<form action="" method="get"><input type="text" name="uid">';
echo "</br><input class='myButton' type='submit' Name='Submit1' VALUE='I have bought'></a></form>";
/** ^ Input value I would like to get *//
echo "<a href='https://www.roblox.com/item-item?id=".$AssetId."' class='myButton'>Buy</a></h3></div></center>";
}
Use the code below to get the value from submit:
if(isset($_GET['Submit1'])) {
echo $_GET['Submit1'];
}
When the user clicks submit, it will echo the value of it.
If you want to print PHP element in a textbox you should put it in the value tag of the input
<?php
echo "<input type='text' value='" . $val . "'>";
?>
I have built a table in PHP to display a list of people and data relating to them. I want the user to be able to select one of those users to send an e-mail request to. So I have put a button at the end of each row for every user.
Im not sure how to write the PHP script to identify which button has been pressed. I have tried assigning the name of the button to be the e-mail address of the user for the row but I dont know how to read that value in the corresponding script...
Code for initial page:
if ($query_run = mysql_query($query))
{
echo "<fieldset>";
echo "<table>";
while ($row = mysql_fetch_assoc($sqldata))
{
echo "<tr><th>Name</th>";
echo "<td>";
echo $row['FirstName']." ".$row['LastName'];
echo "<td>";
echo $row['Country'];
echo "<td>";
echo $row['Profile'];
echo "<td>";
echo $row['Email'];
$Email = $row['Email'];
echo "<td>";
echo '<input type = "submit" name="$Email" value="connect">';
}
echo "</table>";
echo "</fieldset>";
Thanks!
If you are planning to do this in HTML5, you could alter the submit button, so that it would redirect to a page, where you can get the e-mail by $_GET[];
echo "<input type=\"submit\" formaction=\"request.php?email=".$Email."\" value="Submit">\";
Then in your request.php you could use:
$email = $_GET['email'];
I think, you have to change name attribute from "$Email" to, I don't know, maybe "user_email" and assign $email variable to value attribute. So in script which must handle this form you will just need to retrieve a value of $_POST['user_email'].
Also I think it's better to use $Email in double quotes:
echo "<input name='$Email'" ...>;
because you will get string '$Email' instead of expected email address.
Or you can create a form for each table row.
There are any number of ways you could approach this, but I would personally choose to use an HTML form for this purpose. Consider the following HTML:
<form name="myForm" method="GET" action="">
<button type="submit" name="email" value="test1#email.web">test 1</input>
<button type="submit" name="email" value="test2#email.web">test 2</input>
<button type="submit" name="email" value="test3#email.web">test 3</input>
</form>
If you were to try to click on one of these buttons, you'd notice your query string will now show ?email=test2%40email.web (assuming you clicked the second button).
The gist of this approach is to use the same name attribute for your submit buttons, but assign a different value to each. I chose button over input due to the ability to present button text different from the value attribute (as described in this question).
Applying this principle in your PHP, you'd get something like:
while ($row = mysql_fetch_assoc($sqldata))
{
echo "<tr><th>Name</th>";
echo "<td>";
echo $row['FirstName']." ".$row['LastName'];
echo "<td>";
echo $row['Country'];
echo "<td>";
echo $row['Profile'];
echo "<td>";
echo $row['Email'];
$Email = $row['Email'];
echo "<td>";
echo '<button type="submit" name="email_address" value="' . $Email . '">Send email</button>';
}
Then, you just check your email_address parameter for the email address.
I have a form with 2 selects, when you send the first, the second select charges the values that are called on my oracle bd with a query, then when i send the second select, it generates a table with checkboxes:
if(isset($idTActi)){
$stallTableTarifas=oci_parse($conn, "SELECT TARIFAS.ID, TARIFAS.ID_TIPO_ACTIVIDAD, TARIFAS.TIPO, TIPO_ACTIVIDAD.TEMPS_KM, TARIFAS.PRECIO
FROM TARIFAS, TIPO_ACTIVIDAD
WHERE TARIFAS.ID_TIPO_ACTIVIDAD = TIPO_ACTIVIDAD.ID
AND TARIFAS.ID_TIPO_ACTIVIDAD = $idTActi");
oci_execute($stallTableTarifas);
echo "<div class='divPrecios'>";
echo "<table>";
echo "<tr class='tabPreciosTitles'>";
echo "<td>Tipus Tarifa</td>
<td>Temps/Km</td>
<td>Preu</td>
<td><input type='submit' class='carrito' value=''></td>";
echo "</tr>";
while (($row=oci_fetch_array($stallTableTarifas,OCI_BOTH))!=false){
echo "<tr>";
echo "<td>".$row['TIPO']."</td>";
echo "<td>".$row['TEMPS_KM']."</td>";
echo "<td>".$row['PRECIO']."</td>";
echo "<td><input type='checkbox' name='checkbox[]' value='".$row['ID']."'/></td>";
echo "</tr>";
}
echo "</table>";
echo "</div>";
}
echo "</form>";
The variable $idTActi it's the id that i return from the second select, so when i click on the checkboxes and i send it on the button named class='carrito', that's an sprite that i generate on css, i see on the bottom another table with the information that i selected on the previous table:
echo "<div class='divPrecios'>";
echo "<table>";
echo "<tr class='tabPreciosTitles'>";
echo "<td>Nom Activitat</td>
<td>Nom Tipus Activitat</td>
<td>Tipus Tarifa</td>
<td>Temps/km</td>
<td>Preu</td>";
echo "</tr>";
foreach($_POST['checkbox'] as $item){
$stallTableCarrito=oci_parse($conn, "SELECT ACTIVIDAD.NOM AS NOM_ACTIVIDAD, TIPO_ACTIVIDAD.NOM AS NOM_TACTIVIDAD, TARIFAS.TIPO, TIPO_ACTIVIDAD.TEMPS_KM, TARIFAS.PRECIO
FROM TARIFAS, ACTIVIDAD, TIPO_ACTIVIDAD
WHERE TARIFAS.ID = $item
AND TARIFAS.ID_TIPO_ACTIVIDAD = TIPO_ACTIVIDAD.ID
AND TIPO_ACTIVIDAD.ID_ACTIVIDAD = ACTIVIDAD.ID");
oci_execute($stallTableCarrito);
$array=array(
0=>array(),
1=>array(),
2=>array(),
3=>array(),
4=>array()
);
while (($row=oci_fetch_array($stallTableCarrito,OCI_BOTH))!=false){
array_push($array[0],$row['NOM_ACTIVIDAD']);
array_push($array[1],$row['NOM_TACTIVIDAD']);
array_push($array[2],$row['TIPO']);
array_push($array[3],$row['TEMPS_KM']);
array_push($array[4],$row['PRECIO']);
}
for ($x=0;$x<count($array[0]);$x++){
echo "<tr>";
echo " <td>".$array[0][$x]."</td>";
echo " <td>".$array[1][$x]."</td>";
echo " <td>".$array[2][$x]."</td>";
echo " <td>".$array[3][$x]."</td>";
echo " <td>".$array[4][$x]."</td>";
echo " <td><input type='submit' class='carritoElim' value=''></td>";
echo "</tr>";
}
}
echo "</table>";
echo "</div>";
Basically that's a shopping form.
And where is the problem? When i send the pushed checkboxes with the button class='carrito', the form by default refresh the page and clears my array, what can i do?
In your first part of code, is your form tag open ? (I guess it is if this one works)
In the second part, is your <input type='submit' class='carritoElim' value=''> tag in a form ?
Because if it's not, you gonna have a bad time ;-)
Maybe in the last form you should generate hidden input with same names as your first form and same values.
If you don't I guess your variable $idTActi won't be set anymore and it won't succeed the first test if(isset($idTActi)). That could be why you get a cleared page.
If you have a multi step form in the same php page, for this kind of html code :
<form method=POST url="myURL">
<select name="select1">[...]</select>
<select name="select2">[...]</select>
<!-- VARIOUS PART : may not be displayed -->
<div id="checkboxes">
<input type="hidden" name="boxStep" value="1"/>
<input type="checkbox" name="cb1" value="1"/>
[...]
</div>
<!-- END OF VARIOUS PART -->
</form>
Then you need php tests in this order :
// if post request
if (isset($_POST)) {
if (isset($_POST['boxStep'])) {
// behavior when checkboxes values are sent
} else {
if (isset($_POST['select2'])) {
// behavior when second select is filled
// display "VARIOUS PART"
} else {
// behavior when only first select is filled
// Do not display "VARIOUS PART"
}
}
} else {
// default behavior (no select filled)
// Do not display "VARIOUS PART"
}
Apolo