I have read the answer to this question, to execute PHP scripts with the click of a button. But what if I have a "nested button", like this :
<?php
if(!empty($_POST['act'])) {
echo "Ready to rock!";
$someVar = "Rock n Roll";
if(!empty($_POST['act2'])) {
echo $someVar;
} else {
?>
<form method="POST" action="">
<input type="hidden" name="act2" value="run">
<input type="submit" value="Rock It!">
</form>
<?php
}
} else {
?>
<form method="POST" action="">
<input type="hidden" name="act" value="run">
<input type="submit" value="Show It!">
</form>
<?php } ?>
I heard my problem can be solved with jQuery, but I no idea.
anyone please.
To execute a script on the server you use the action property of your form:
<form method="POST" action="myscript.php">
When clicking a input type="submit" the browser will go to to action of the form surrounding the input type="submit"
Nesting is not a issue, as the browser always will look for the 'surrounding' form.
Problem is in second form, so it will never calls in this code, because it fails in first $_POST variable IF statement, because in second form you do not POST variable "act". so you need to add it
<form method="POST" action="">
<input type="hidden" name="act" value="run">
<input type="hidden" name="act2" value="run">
<input type="submit" value="Rock It!">
</form>
with this form you should see echo $someVar;
p.s. if form action property is emtpy, by default it submits form to the same php script
Just like #DTukans said here, you need the hidden field. If you would post the second form, the value of act will be lost if you are not having a hidden field with the value of act from the first form.
In php you can also check which submit button you submitted by giving the input[type="submit"] a name, such as <input type="submit" name="form2">, then you could check if you submitted that form by:
if (isset($_POST['form2'])) {}, but this is not the case here.
Use the hidden input and you will be good to go.
Related
I am unable to find a simple working php form anywhere on the net. There is plenty of forms that are in the same file as the rest of the HTML code, but it is just plain confusing when you have 2 or 3 forms in the same index.html file.
What I'm looking for is just a basic HTML form with method='post' and action attribute set to action='action.php' , and then making it work in that .php file.
You can put HTML code in PHP file. Here is an example how the code works.
HTML
<form action="post" action="action.php">
<input type="submit" name="test"/>
</form>
To check if a form has been submitted or not, use this
PHP
if ($_POST['test']) {} // the key "test" should be the same as the name "test" in input type submit.
Consider you here three form in single html/php page like
<form action="post" action="action.php">
<input type="submit" name="signin_form"/>
</form>
<form action="post" action="action.php">
<input type="submit" name="signup_form"/>
</form>
<form action="post" action="action.php">
<input type="submit" name="password_reset_form"/>
</form>
you can see the name attribute on submit button now when user will submit the form the $_POST super global will have the value of submitted button name. and you can check this using this something like
<?php
if(isset($_POST['signin_form']))
{
// user submitted the sign in form
}
if(isset($_POST['signup_form']))
{
// user submitted the sign up form
}
if(isset($_POST['password_reset_form']))
{
// user submitted the password reset form form
}
?>
you can also add a hidden field in each form with the value of form name like
<form action="post" action="action.php">
<input type="hidden" name="form_type" value="sign_in"/>
<input type="submit" value="submit"/>
</form>
<form action="post" action="action.php">
<input type="hidden" name="form_type" value="sign_up"/>
<input type="submit" value="submit"/>
</form>
<form action="post" action="action.php">
<input type="hidden" name="form_type" value="password_reset"/>
<input type="submit" value="submit"/>
</form>
now you can check in same manner
<?php
if(isset($_POST['form_type']) && $_POST['form_type'] == 'sign_in')
{
// user submitted the sign in form
}
if(isset($_POST['form_type']) && $_POST['form_type'] == 'sign_up')
{
// user submitted the sign up form
}
if(isset($_POST['form_type']) && $_POST['form_type'] == 'password_reset')
{
// user submitted the password reset form form
}
?>
I have a problem of getting the value of an input element. Thanks in advance for help! This is the sample code:
<form action="#" method="POST">
<input name="X" value="3" />
<?php
//question: how can I put the value of input in a variable
//$_POST['X']; isn't applicable since I am in the same form.
?>
</form>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
<input name="X" value="3" />
<input type="submit" value="Submit">
</form>
<?php
if(isset($_POST['X']) == true){
echo $_POST['X'];
}
?>
First of all submitting a form on its own page is a bad practice because whenever you reload the page the form will submit everytime.
Best approach is that you set the action attribute of form to another file or link.
fileOne.php (where your form is located):
<form action="anotherfile.php" method="POST">
<input name="X" value="3" />
<button type="submit" name="submitForm">Submit</button>
</form>
Then in anotherfile.php you will do this:
$check = $_POST["submitForm"];
If(isset($check))
{
$myInputValue = $_POST["X"];
header("Location: fileOne.php/?val".$myInputValue);
}
Then in fileOne.php you can get the variable and its value which is sent via link in the browser like this:
$getInputValue = $_GET["val"];
Now you can echo out the $getInputValue variable wherever you want.
If the the information is not sensitive this is the best approach you got. But if it is, try saving that value in the session and retrieving in the file where you want. I hope it helps. Typed code via app so it might throw an error. But i think this code will work fine. Good luck!
I have one page with a simple post form
<form name="click" action="UserOrders.php" method="post">
<input type="hidden" name="amount" value="<?php echo $total ?>">
<input type="image" src="/Templates/images/UpdateButton.png" name="submit">
</form>
and the only thing I want to check is if it submits.. In the other page "UserOrders.php" I just wrote
<?php
if ($_POST['submit']){
echo $_SESSION['ID'];
}
It seems to me irregular that it doesn't work and I would like another set of eyes to check it out.
(if i put the echo $_SESSION['ID'] outside the brackets, it works.)
An input type="image" only defines that image as the submit button and not as an input that can carry over a value to the server. source
An alternative way to see if the form was submitted is to check $_SERVER['REQUEST_METHOD']
if ('POST' === $_SERVER['REQUEST_METHOD']) {
// submitted
}
I agree with the previous answer. You are not actually putting any information to the form and therefore there is nothing for the new form to react to. $_POST['submit"] is looking for data that was sent and not for the method of sending.
There's no actual submit button. You need a
<form action="UsersOrders.php" method="POST">
<input type="whatever you want" name="input">
<input type="submit" name="submit">
</form>
Now if you go into UsersOrders.php and do what you did before:
if($_POST['submit']) {
echo $_POST['input'];
}
You will actually get some input.
I have a simple registration form.
I want to pass value entered in one page to other in a text field.
how to pass and access it from next page in php.
this is my first php page.
Thanks in advance.
You can add hidden fields within HTML and access them in PHP:
<input type="hidden" name="myFieldName" value="someValue"/>
Then in PHP:
$val = $_POST['myFieldName'];
If you're going to ouput this again you should use htmlspecialchars or something similar to prevent injection attacks.
<input type="hidden" name="myFieldName" value="<?=htmlspecialchars($_POST['myFieldName']);?>"/>
Suppose this the form input in page A
<form name="" action="" method=post enctype="multipart/form-data">
<input type="text" name="myvalue" value="">
<input type=submit>
</form>
In page B
In the page you want to get values put this code
<?PHP
foreach ($_REQUEST as $key => $value ) {
$$key=(stripslashes($value));
}
?>
<form name="" action="" method=post enctype="multipart/form-data">
<input type="text" name="myvalue" value="<?PHP echo $myvalue" ?>">
<input type=submit>
</form>
So yo can use or attach variable value to another form do what else you want to do
use following code, that should help you.
<form action ="formhandler.php" method ="POST" >
<input name = "inputfield" />
<input type="submit" />
</form>
on formhandler.php file yo need to enter following code to get the value of inputfiled.
$inputfield = isset($_POST['inputfield'])?$_POST['inputfield']:"";
// now you can do what ever you want with $inputfield value
echo($inputfield);
in order to edit my entries i want to:
<form id="pregunta" name="pregunta" class="form_pregunta" method="post" action="pregunta.php?id=26">
<h2>Titulo de la pregunta</h2><input name="q" id="q" class="q" value="este es mi tÃtiulo " type="text">
<h2>Describe tu pregunta</h2>
<textarea name="texto" id="texto" style="width: 98%;"><p>esta es mi descripcion</p></textarea>
<h2>Etiquetas</h2>
<input name="tags" id="tags" onmouseover="mostrar_tooltip('nube_e','','0','70','')" onmouseout="ocultar_tooltip('nube_e')" value="dos,tres,una,">
<input name="responde_a" style="display: none;" id="responde_a" value="0">
<button name="pregunta" id="pregunta" type="submit">form_edit_question_button</button>
</form>
And then in file.php
i'd like to $_get['id'] and $_post['inputs']
but when i go:
if(isset($_POST['edit_pregunta'])){
echo 'lalalalalalalalalalalalalalala';
post_edit_pregunta();
}
it won't ever enter :S. is that normal or i'm missing something... i wanted not to have a hidden input with the id of the post i want to edit..
I'm not 100% sure, but forms don't send their name when submitted, much less their id.
You could do the following instead:
<form id="edit_pregunta" method="post" action="file.php?id='$this->id'">
<input type="hidden" name="edit_pregunta" value="anything">
... //inputs here
</form>
and your if should now enter.
It looks like you're checking for your form's "id" attribute. This is not sent when the form is submitted, only values in <input>, <select>, <textarea> and <button> are sent.
You should check for one of those.
Edit: Your button name is "pregunta", so that is the POST variable you should be checking for, eg
if(isset($_POST['pregunta'])){
Just to comment in general on mixing params in the form's "action" and inputs, you can mix them as long as the form method is "post". You cannot set GET params in the form's action and use the "get" method
<!-- Good -->
<form action="proc.php?id=123" method="post">
<input name="foo" value="foo">
<input type="submit">
</form>
<!-- Bad -->
<form action="proc.php?id=123" method="get">
<input name="foo" value="foo">
<input type="submit">
</form>
There should be no problem at all with having get and post variables in the same request, but are you sure your syntax is correct? If this is normal php, shouldn't you write
<form id="edit_pregunta" method="post" action="file.php?id=<?php echo $this->id; ?>">
... //inputs here
</form>
[Edit]
The problem is (if I'm correct and this is standard php) that you generate a form that looks something like this:
<form id="edit_pregunta" method="post" action="file.php?id='$this->id'">
... //inputs here
</form>
This will make id look like this: '$this->id' (including the '-signs). When what you want is something like this:
<form id="edit_pregunta" method="post" action="file.php?id=51">
... //inputs here
</form>
Then $_GET['id'] would be 51.
[Edit2]
Also, I think you need to change
if(isset($_POST['edit_pregunta'])){
with
if(isset($_POST['pregunta'])){
If I'm not mistaken the name of a form doesn't get sent to the server, however, the name of the submit-button does, but I might be wrong about that part.
Yes you can, I've done it several times.
Probably something else is wrong with your code.
Is there any control with name="edit_pregunta" or is it just the id of the form? IDs are not sent to the server.
Simply adding the id to the form will not create the $_POST['edit_pregunta'] you verify.
Instead, inside the form tag, add an <input name="foo" />; in the php script verify $_POST['foo']
While the HTTP spec doesn't disallow query parameters in POST methods, it is somewhat unusual. You'd be better off using a hidden input field in the form to pass any non-user values up to the script.
That said, the syntax for your form is wrong. You need to use "echo" to insert the value of $this->id into the action.
Use input type="submit" in place of button tag.
You need name for form submission and activate php script!
HTML Code:
<form action="change.php" method="POST">
<input type="password" name="p1" class="change_text" placeholder="New Password"/></br>
<input type="password" name="p2" class="change_text" placeholder="Re-Password"/></br>
<input type="submit" name="change" value="Change Password" id="change" />
</form>
PHP Code:
<?php
if (isset($_POST['change']) {
$p1=$_POST['p1'];
}
?>