I have a problem when developping a page in php, when click on a submit button another submit button. appear.
my problem appeared at the isset of this submit button, echo $b doesn't work; here is my code:
<form method='POST'>
<input type="submit" name="s1">
</form>
</body>
</html>
<?php
if(isset($_POST["s1"])){
$b=2;
echo "<form method='POST'><input type='submit' name='s2'></form>";
if(isset($_POST["s2"])){
echo $b;
}
}
I've already tried to make $b a global variable, but no change :(
Thank's for your help.
The flow of your code is first send the value of input s2, if this was set with some value echo your second form and set b variable to 2, when your second form is printed out on the browser the b value will have lost, so your must store this value in some place, this could be done through a hidden field in your second form like this:
echo "<form><input type='text' name='s2'><input type='hidden' name='b' value='$b'></form>"
if(isset($_POST["s2"])){
echo $_POST['b'];
}
Hope this helps you!
Related
I created a form which asks for username and password for registration purposes and I sent the data to same page using action="" and checking for $_POST variables, but data is not being passed through this method. When I print $POST array by changing condition to true and reloading the page , the POST array is empty and also I can see the variables passed as POST in URL.Can somebody explain whats the problem?
Code:
<!DOCTYPE html>
<html>
<?php
if(isset($_POST['user']))
{
echo "Data coming";
die();
}
else {
?>
<form method="POST" enctype="multiform/form-data" action="">
<b> Username: </b><input type='text' name='user'> <br> <br><br>
<b> Password:</b> <input type='password' name='pass'><br> <br>
<input type='submit' value="Submit">
</form>
<?php
}
?>
</html>
You have to give the name to submit button also.
<input type='submit' name="Submit" value="Submit">
You are checking the index 'Submit' in $_POST and the submitted form is take the value with the name of input type. So you have to given the name to submit button also like above.
You are checking in your if isset for a post named:
$_POST['Submit']
Your submit button doesn't have
name=Submit
As a test to see what you are sending for debug purposes, try putting this in your code, it will help you self-debug:
print_r($_REQUEST);
"input type='submit' name="Submit" ...
Change to this.
$_POST['Submit']
looks for a resource with the name of Submit when you reload the page, as in your code there is no resource with the name of Submit, the if statement returns false and the control switches back to the else part.
Give a name to the submit button and use the same name in the post varibale option.
What wrong with my code? uniqid() is for generating a unique code and it will be stored to the name attribute then a condition is stated if it is clicked it will generate it's working.. Could some help me please with this one? Thanks in advance..
<html>
<form method="POST">
<?php
$attname = uniqid();
echo "<input type='submit' name='$attname' class='btn btn-success' value='Post to PeĆ³nline'/>";
echo $attname;
if(isset($_POST[$attname])){
echo 'its working';
}
?>
</form>
<html>
This isn't going to work.
When you refresh the page the $attname value will change. This will happen when you submit the form. So the actual name you're checking on will change and not be the same as the new $attname.
Put the following after your echo $attname; line:
print_r($_POST);
Also for this to work properly you'll need to nest the <input> tag in a <form> tag e.g.:
<form method="POST">
<input>...</input>
</form>
The $attname will change after page refresh.
You need to store that name somewhere.
Add another hidden element.
...
echo "<input type='hidden' name='elemname' value='<?php echo $attname;?>'/>";
...
And after submit,
if (isset($_POST['elemname'])) {
$elemname = $_POST['elemname'];
$val = isset($_POST[$elemname]) ? $_POST[$elemname] : '';
echo $val;
}
I have a few Radio Buttons which shell send their value in the post method, when the submit button is clicked. I am pretty sure it is easy, but for any reason it does not work. It sends the calue of the submit button instead. Please help me. This is my code for now(this code is inside a PHP script):
echo "<form action=\"nutzerverwaltung.php\" method=\"post\">";
echo "<table [...]";
while ($row = $alluser->fetch_assoc())
{
echo "<tr>
HERE---> <td><input type=\"radio\" name=\"select_to_delete\" value=".$row["id"]."></td>
<td>".ucwords(strtolower(str_replace(".", " ", $row["username"])))."</td>
<td>".$row["username"]."#via-ev.de</td>
<td>".$row["permissionlevel"]."</td>
</tr>";
}
echo "</table><br />";
echo "<input type=\"submit\" name=\"select_to_delete\" class=\"nv_button\" />
</form>";
Putting my comments to an answer, since it was clearly the issue. As posted 17 mins. prior to my answer.
Both your radio and submit form elements hold the same name attribute.
I would call that a collision/conflict. Rename one, mainly your submit button.
You can have radio buttons holding the same name attribute as an array, but not the submit button. Elements of the same "group" can have the same name attribute.
your submit input have the same name attribute just remove that
echo "<input type=\"submit\" class=\"nv_button\" />
in html forms if you have inputs with same name the last one value will be send as your value
I have the following chunk of code:
while ($row = mysql_fetch_array($result)) {
echo "<li/>".$row['trendName']."<input type='submit' name='Dispay' value='Display All Items' onclick='action='http://blah.co.uk/DisplayData.php';'/>";
echo"<br />";
}
So, this I want to do is for each element on the list to create a button. When I will click at this button I want to hit a URL.
Let's say that we have only one element on the list (Movies). By clicking on the button next to the movies, I want to move to another URL: http://blah.co.uk/DisplayData.php
and furthermore to pass to the other URL the name of the element (i.e. Movies)
Is this possible, and if so, how?
I would also recommend to use <button></button> in html 5 to create a button.
echo "<button type='submit' name='Display' onclick='javascript:location=\"http://blah.co.uk/DisplayData.php?value=$row['trendName']\"'>Display</button>";
As suggested above, a link is the proper way to do so.
<?php echo "<a href='http://blah.co.uk/DisplayData.php?value=".$row['trendName']."'>Display</a>"; ?>
You can use $_GET['value']; to retrieve your data "trendName"
However "action" is used in the <form></form> tag
<form action='http://blah.co.uk/DisplayData.php'>
<input type='hidden' name='value' value='<?php echo $row['trendName']; ?>' />
<button type='submit'>Display</button>
</form>
here you would use $_POST['value']; to retrieve your data "trendName"
onclick="this.location.href='http://blah.co.uk/DisplayData.php?value=' + $row['trendName']"
If you tell, where "someValue" should come from, I'll edit this code later.
However, I would recommend using a link instead.
Hey, I've got what has become an extremely frustrating problem with $_Post variables. I'll give examples of code rather than the actual segments to save time and confusion. On one page I'm doing this:
<? echo "<form name='form' action='page.php' method='post'>
<input type='hidden' name='slot' value=".$i.">
</form>";
?>
The $i is an index in a while loop (I'm echoing this simple form several times). The form itself is submitted with a bit a javascript.
All's well at this point, the form is submitted properly and takes me to another page, where I need to use that "slot" value to do some other junk. However, when I try to do this:
<? echo "<form name='another_form' action='another_page.php' method='post'>
<input type='hidden' name='slot_num' value=".$_POST['slot'].">
//SOME OTHER HIDDEN VARS
</form>";
?>
or this...
<? echo "<form name='another_form' action='another_page.php' method='post'>
<input type='hidden' name='slot_num' value=";
echo $_POST['slot'];
echo ">
//SOME OTHER HIDDEN VARS
</form>";
?>
or this...
<? //TOP OF PAGE
$slots = $_POST['slot'];
?>
<? //FURTHER DOWN
<? echo "<form name='another_form' action='another_page.php' method='post'>
<input type='hidden' name='slot_num' value=".$slots.">
//SOME OTHER HIDDEN VARS
</form>";
?>
...all I get is an Undefined index: slot etc.. etc... error, and source of the php document just has blank space. Funny thing is, if I simply do this:
echo $_POST['slot'];
at the top of the page, it prints out the value from the previous page just fine, however, if I view the source, it still shows an Undefined index error instead of the value. I KNOW the value is getting passed because it prints, but I can't use it for anything else because if I try to include it in my php code, it just displays an error and gives a blank value!
I've also tried using $HTTP_POST_VARS['slots'] with the same result... I am at wits end after several hours of experimentation... any advice?
check for emptiness:
if(empty($_POST['foo'])) {
$foo = "default foo";
} else {
$foo = $_POST['foo'];
}
print "My foo is '$foo'";
Edit:
Based on your comments and posted code, you seem to be trying to echo $_POST['slot'] when you should be echoing $_POST['slots']... note the s at the end.
Since $_POST is a super global, it is available anywhere on your page, so your code should work.
I noticed that you mixed slot and slots as the index in you post (you wrote $HTTP_POST_VARS['slots'] as the last example and $_POST['slot'] everywhere else). Could that be the reason?
To check what $_POST looks like, try this right about where you want to print the hidden value (though it should work the same anywhere on your page):
<?php
echo "<pre>";
print_r($_POST);
echo "</pre>";
?>
Also, your slot isn't being echoed with quote marks around it, so it should be:
<?php echo "<form name='another_form' action='another_page.php' method='post'>
<input type='hidden' name='slot_num' value='".$_POST['slots']."'>
//SOME OTHER HIDDEN VARS
</form>";
?>
Well I can't see anything wrong apart from a few syntax problems... okay so first of all, can you post me your javascript so I can see that.
Now instead of what you are doing with that, try this code:
?> <form name="another_form" action="another_page.php" method="post">
<input type="hidden" name="slot_num" value="<?php echo isset($_REQUEST['slot'])?$_REQUEST['slot']:'not found'; ?>">
</form>
<?
I am not fully sure what is wrong but i don't think the problem is in the code you've posted. it's probably sitting elsewhere so keep sending through code.
Try replacing...
<? //TOP OF PAGE
$slots = $_POST['slot'];
?>
with..
<?php //TOP OF PAGE
isset($_POST['slot']) : $slots = $_POST['slot'] ? $slots = '';
?>
Best of luck, hope that helps!