Is it possible to control which form a submit button calls? - php

so I'm working on a site, I currently have:
<form method =post action=process.php>
<form method = get action = process3.php>
<input type=submit value = add name = action/>
</form>
</form>
is it possible to add a attribute to control which form this input calls?
For reasons un-mentioned above I am unable to simply use just one method

You cannot nest forms like you do. You can submit forms from outside.
<form id="myform1" name="form1" action="" method="post">
<input name="field1" value="form1" />
</form>
<form id="myform2" name="form2" action="" method="post">
<input name="field2" value="form2" />
</form>
<input type="submit" form='myform1' value="submit1" />
<input type="submit" form='myform2' value="submit2" />
You can also add input fields outside the form tag
add input to form 2:
<input type="text" name='outside' value="outside" form='myform2' />

No. Forms can't be nested like this. That is invalid HTML. Moreso, its impossible to GET and POST data at the same time.
The submit action is strongly tied to its parent form, so I'd recommend writing your forms as siblings, then putting the field in the appropriate one and giving each form its own submit input.
<form method =post action=process.php>
<input type=submit value = add name = differentAction/>
</form>
<form method = get action = process3.php>
<input type=submit value = add name = action/>
</form>

Unless you are using way more sophisticated methods for controlling your forms the best way is to keep your submit action within the corresponding <form> </form> tag.
This might prove to be helpful depending on what you are trying to accomplish:
Submit multiple forms with one submit button
and this:
http://jquery.malsup.com/form/#getting-started

Related

If have two forms (or more). Is it possible to fetch which form has sent the upload request?

Form 1:
<?php
echo $this->upload_message;
?>
<form enctype="multipart/form-data" method="post" name="mfuploaderwp-uploadform" action="?uploadfile">
Upload file: <input name="mfuploadwp-filename" type="file">
<input class="mfuploadwp-submit" type="submit" value="Upload" name="submit"></form>
Form 2:
<?php
echo $this->upload_message;
?>
<form enctype="multipart/form-data" method="post" name="mfuploaderwp-uploadform" action="?uploadfile">
Upload file: <input name="mfuploadwp-filename" type="file">
<input class="mfuploadwp-submit" type="submit" value="Upload" name="submit"></form>
Is it possible to fetch which form is submitted without giving any extra attributes or so to above forms? The forms are created dynamically based on what user enter for amount number of forms. (In this case user has entered 2 forms)
I want to do this so $this->upload_message would be accurate only for the form that is used for uploading.
Alter the name tags on your <input type="submit"> buttons. Have one as name="submit" and the other as name="submit_two" (for example, bad naming convention), then process code as
if (isset($_POST['submit'])) {
// do stuff
} elseif (isset($_POST['submit_two'])) {
// do other stuff
}
Yes, it's possible.
The cleanest way, in my opinion, is to put an hidden input tag in each form:
<form enctype="multipart/form-data" method="post" name="mfuploaderwp-uploadform" action="?uploadfile">
<input type="hidden" name="active_form" value="1">
(...)
and
<form enctype="multipart/form-data" method="post" name="mfuploaderwp-uploadform" action="?uploadfile">
<input type="hidden" name="active_form" value="2">
(...)
then, in the page that process the form, you can check it in this way:
if( $_POST['active_form'] == 1)
{
(...)
}
elseif( $_POST['active_form'] == 2)
{
(...)
}
If your form is generated dynamically based on the user input(The forms are created dynamically based on what user enter for amount number of forms), in this case you can use three type of solution as far as I know,
You can introduce a new hidden field for each form based on the form number.
Eg:
Upload file: <input name="mfuploadwp-filename" type="file">
<input class="mfuploadwp-submit" type="submit" value="Upload" name="submit">
<input type="hidden" value="1" name="form_id"/>
</form>
in php
switch($_POST['form_id']) {
//the form data to be processed..
}
or
You can update the input field submit button naming based on the form number.
Eg:
Upload file: <input name="mfuploadwp-filename" type="file">
<input class="mfuploadwp-submit" type="submit" value="Upload" name="submit_{form_id}">
you can add a additional parameter in the form method.
...

A second button to start php script, how?

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.

passing value in hidden field from one page to another in php

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);

Can i Mix $_get and $_post?

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'];
}
?>

send the form value into the form action parameter

Can i send a input value into form action ?Let say, on the form the phone number taken.Now can i send the phone number as form action parameter "number"? Is their any way to send it?
<form method="post" action="abc.php?number=ph_number" enctype="multipart/form-data">
<input type="text" name="ph_number" value=""/>
<input type="submit" name="search" value="SEND"/>
</form>
How can i do it?
Thanks in advance
riad
<form method="GET" action="abc.php" enctype="multipart/form-data">
<input type="text" name="number" value=""/>
<input type="submit" name="search" value="SEND"/>
</form>
Change action="abc.php?number=ph_number" to action="abc.php
Change name="ph_number" to name="number"
When you click submit, the value contained in "number" text field will be passed to abc.php.
Receive the value with $value = $_REQUEST['number']; in abc.php.
You can leave an empty action, and use the onSubmit event to load a javascript function that does whatever and redirects to the page according to the input value.
Html
<form .. action="" onsubmit="return abcByPhone(this);">
Javascript
function abcByPhone(form) {
url = from.number.value;
...
}
EDIT:
I actually didn't read the question properly. I thought you wanted to redirect to different pages according to the input. Using plain GET (like the others mentioned) is fine for this.

Categories