PHP form submission with echo html post's empty array - php

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

Related

html+php+database (very beginner)

I'd like to ask for help about my code. I'm still beginning to find way with html, php, databases and all that. This is something I already did some times, but somehow I got stuck at this point.
The connection is fine, so I omitted it.
The thing is that my buttons don't do anything when I click them, it's like there's no action to be taken. At this point I can't notice my mistake, but I'm sure it's very simple. :(
I've translated some things from the code from my native language to English, so if you find it inconsistent in that regard, I'm sorry. *
I'd guess it's something I've messed up on the table structure
Thank you!
<?php
echo "<th>City |</th>";
echo "<th>Update |</th>";
echo "<th>Delete |</th>";
echo "</tr>";
$query = "SELECT name, id_city as id FROM city";
$result = pg_query($conn, $query);
if($result) {
while($row = pg_fetch_assoc($result)) {
echo '<tr>';
echo '<td>';
echo $row['name'];
echo '</td>';
echo '<td>';
echo '<form method="post" action="./updatecity.php">';
echo '<input type="hidden" name=id_city value="'.$row['id'].'">';
echo '<input type="submit" name="submit" value="Update">';
echo '</form>';
echo '</td>';
echo '<td>';
echo '<form method="post" action="./deletecity.php">';
echo '<input type="hidden" name=id_city value="'.$row['id'].'">';
echo '<input type="submit" name="submit" value="Delete">';
echo '</form>';
echo '</td>';
echo '</tr>';
}
}
pg_close($conn);
?>
Your approach to making form is completely wrong, what you are doing in simply echoing texts, No form is getting generated. Hence the button is not working because there is no button.
What you need to do it something like this :
<?php
#Write your php related code here
#Like connecting to database
?>
#All your html related content goes here like making tables
<form method="post" action="./updatecity.php">
<input type="hidden" name=id_city value="<?php echo $row['id'] ?>">
<input type="submit" name="submit" value="Update">
</form>
<?php
//Write your php related code here
?>

Find the variable generated in loop of php

I have this php code:
for($i=0;$i<3;$i++) {
echo 'Question'.$i.'</br>';
echo 'Answer..</br>';
echo '<form action="" method="POST">';
echo '<textarea name="answer"></textarea>';
echo '</br><button name="answer_button'.$i.'"><b>Answer</b></button>';
echo '</form>';
}
Now I want to get the question number for which the answer_button is clicked.
Closest I could get was this:
for($i=0;$i<3;$i++) {
echo 'Question'.$i.'</br>';
echo 'Answer..</br>';
echo '<form action="" method="POST">';
echo '<textarea name="answer"></textarea>';
echo '</br><button name="answer_button"><b>Answer</b></button>';
echo '</form>';
if(isset($_POST['answer_button'])) {
echo $i;
break;
}
}
This gives me Question number but it will not print other questions in loop once the button is clicked.
Is there no solution without using javaScript?
This should work :
for($i=0;$i<3;$i++) {
echo 'Question'.$i.'</br>';
echo 'Answer..</br>';
echo '<form action="" method="POST">';
echo '<textarea name="answer"></textarea>';
echo '</br><button name="answer_button'.$i.'"><b>Answer</b></button>';
echo '</form>';
echo '</div>';
}
for($i=0;$i<3;$i++) {
if(isset($_POST["answer_button".$i])) {
echo $i;
}
}
Hope it helps.
Change the html markup to use an "array notation":
echo '<button name="answer_button['.$i.']" type="submit">Answer</button>';
That will cause php to populate an array when receiving back the form which you can examine:
<?php
// ...
if(isset($_POST['answer_button']) && is_array($_POST['answer_button'])) {
$id = array_shift(array_keys($_POST['answer_button']));
// ...
}
This allows to have multiple such buttons in a single form and detect which one has actually been clicked. It works, because $_POST will contain an array with a single element with key as in $id, which you can easily examine yourself with a var_dump($_POST); or similar.
I think this is what your looking for.
CODE
<?php
echo '<form action="" method="POST">';
for ($i = 0; $i < 3; $i++) {
echo 'Question: ' . $i . '<br>';
echo 'Answer..<br>';
echo "<textarea name='answer[$i]'> </textarea></br>";
echo "</br><button name='answer_button[$i]' value='BtnPushed'> <b>Answer</b></button><br>";
if (! empty($_POST['answer_button'][$i])) echo "Last Answer: {$_POST['answer'][$i]}<br>";
echo '<hr>';
}
echo '</form>';
Results

How can I get the sender ID of dynamically created submit buttons?

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

How to pass 2 radio selection value with a single Submit??

echo '<form method="post" action="mod_slots.php">';
$i = 1;
while(cond)
{
echo '<input type="radio" name="change1" value="'.$i.'" />';
$i++;
}
$j = 1;
while(cond)
{
echo '<input type="radio" name="change1" value="'.$j.'" />';
$j++;
}
}
echo '<input type="submit" value="Change Selected" />';
echo '</form>';
The above code passes only change1 value to the next page! But I want a single submit button, which will send both value!!??
My implementation may be wrong!
Radios with same name could only have one selection, if you want to divide them, just use different name.
Either you can pass them by giving their names as array like
echo '<input type="radio" name="change[]" value="'.$i.'" />';
and after submit you can print them like
print_r($_POST['change']);

passing variables through session - php

I'm new to php and I'm trying to pass a the value of a submit button through a session.
my code so far looks like this:
main_page.php
session_start();
echo '<form method="post" action="details_page.php">';
echo '<li><a href="ifconfig.php> <input type="submit" name=submit value='."$ip_address".' /></a></li>';
echo '</form>';
$value_to_pass = $_POST[submit];
echo $value_to_pass;
$_SESSION['value'] = $value_to_pass;
}
details_page.php
session_start();
echo $value_to_pass = $_SESSION['value'];
echo $value_to_pass;
I need it to print $value_to_pass in the details_page.php
This is highly confusing
session_start();
echo '<form method="post" action="details_page.php">';
echo '<li><a href="ifconfig.php> <input type="submit" name=submit value='."$ip_address".' /></a></li>';
echo '</form>';
$value_to_pass = $_POST[submit];
echo $value_to_pass;
$_SESSION['value'] = $value_to_pass;
Consider changing it to this so that your POST code only gets executed when the form is actually submitted. Without this check your SESSION will be assigned a blank value when the form is not submitted which may be giving you odd results.
session_start();
echo '<form method="post" action="details_page.php">';
echo '<li><a href="ifconfig.php> <input type="submit" name=submit value='."$ip_address".' /></a></li>';
echo '</form>';
// Note also you need single quotes in the $_POST array around 'submit'
if(isset($_POST['submit']))
{
$value_to_pass = $_POST[submit];
echo $value_to_pass;
$_SESSION['value'] = $value_to_pass;
}
And change details_page.php to
session_start();
// Do not echo this line, as you are echoing the assignment operation.
$value_to_pass = $_SESSION['value'];
var_dump($value_to_pass);
first You change your below code
echo '<form method="post" action="details_page.php">';
echo '<li><a href="ifconfig.php> <input type="submit" name=submit value='."$ip_address".' /></a></li>';
echo '</form>';
with the below
echo '<form method="post" action="details_page.php">';
echo '<input type="hidden" name="ip_address" id="ip_address" value="'.$ip_address.'">';
echo '<li><a href="ifconfig.php> <input type="submit" name=submit /></a></li>';
echo '</form>';
this is the best practice actually. I also followed this. So try to avoid passing values in "submit" button. pass it to "hidden" field.
and then get the value like below:-
$value_to_pass = $_POST["ip_address"];
I think it will help You. Thanks.

Categories