Query String in form does not work - php

I have the following form in my form.php file:
<form action="operation.php?part=dictionary&operation=<?php echo (($_GET['action']=='addword')?'save':'edit&id='.$_GET['id'])?>" method="get">
.....
....
</form>
And in my operation.php file:
if($_GET['operation']=='save'){
echo "This is true";
}else{
die(mysql_error());
}
And it shows the message that it does not recognize the operation parameter.
So if anyone have any idea of how to distinguish the operation between save and edit would be really appreciated. Thanks you

You can try using:
<form action="operation.php" method="get">
<input type="hidden" name="part" value="dictionary">
<input type="hidden" name="operation" value="<?php echo (($_GET['action']=='addword')?'save':'edit&id='.$_GET['id'])?>">
</form>

Setting a form's method to "GET" results in ignoring all GET-parameter added to the action of the form. In order to get those parameter to work you will have to add them as hidden input fields otherwise you switch your form's method to "POST". This results in setting POST-parameter according to form fields and setting GET-parameter according to the link additions made at form's action.

You need to use hidden parameters to submit values to your form, like so:
<form action="operation.php" method="GET">
<input type="hidden" name="part" value="dictionary" />
<input type="hidden" name="operation" value="<?php echo (($_GET['action']=='addword')?'save':'edit&id='.$_GET['id'])?>" />
</form>

Related

HTML post form with preset variables

I'll try and explain this as best as I can. Basically, I'm using a form to receive a comment. Upon hitting submit, the action creates a link similar to this: http://localhost:8080/camagru/comment.php?comment=test&post=Post
I have a variable with the image name in it that I want to pass as well, so something like this: http://localhost:8080/camagru/comment.php?img=test.png&comment=test&post=Post
I've tried using <form action="<?php echo commentpost.php?img=$img?>"> But everytime the submit button is pressed, it erases the img variable from POST and only puts in the new variables from the form.
Any suggestions?
add new hidden field in form tag like that
<form action="commentpost.php" method="post">
<input type="hidden" value="<?php echo $img ?>" name="img" />
<input type="submit" value="Save" name="IsSubmit" />
</form>
Now you can able to use $_POST['img']
The img variable is in GET.
If you want it in POST, try <input type="hidden" name="img" value="test.png">
use quotes in your case:
<form action="<?php echo "commentpost.php?img=$img"; ?>">
the best practice is to insert hidden element into your form:
<input name="img1" type="hidden" value="test.png" />
<input name="img2" type="hidden" value="test2.png" />

How to send a get form wthout losing get parameters

I have a form located at a url containing get parameters,my form is also using this method.When the form is submitted it rewrites the previos get parameters.
Is there a simple way to rewrite only my form parameters?
I have in mind a Javascript solution ,however I want to know if there is a simpler way?Using HTML/PHP perhaps?
As far as I know, u u are not interested in using JS, then using form's hidden element is only way u have like this-
<form action="demo_form.asp">
First name: <input type="text" name="fname"><br>
<input type="hidden" name="country" value="Norway">
<input type="submit" value="Submit">
</form>
<p>Notice that the hidden field above is not shown to a user.</p>
The question is how u can use it with PHP, right?
The solution is here-
//In PHP
if( isset($_GET['fromPerson']) )
{
echo $fromPerson;
}
So combined HTML and PHP code will be like this (assuming a get element from prevous page is named fromPerson)-
<form action="demo_form.asp">
First name: <input type="text" name="fname"><br>
<?php
if( isset($_GET['fromPerson']) )
{
echo '<input type="hidden" name="country" value=".$_POST['fromPerson'].">';
}
?>
<input type="submit" value="Submit">
</form>
Lets say you get a parameter p1 from a get request, it should look like this:
http://server.com/?p1=123
In your form, you can add hidden fields that would have the same effect when you submit, like this:
<form method="GET">
<input type="hidden" value="<?php echo $_GET["p1"]; ?>" name="p1">
</form>
That way you can resend the variables as many times as you need.
I'm not sure I understand your question... Can you post your code?
I assume you mean something like this?
in index.php
<input type="hidden" name="id" value="<?php echo $id; ?>" />
in return.php
Edit

How can the action at the form get the value?

When I try to use this structure:
<form name="confirm" method="get" accept-charset="utf-8" enctype="multipart/form-data" action="confirm_sent.php?name=<?PHP echo $name; ?>">
...........//sth doing at here
<input type="submit" class="popUpButton" name="confirmButton" id="submitPage4" value="Confirm" />
The name was get from the URL:
http://localhost/reserve/app/confirm.php?name=$name
But, when I click the button confirmButton it will directly to confirm_sent.php
but at the URL there the value of name does not show out only show this:
http://localhost/reserve/app/confirm_sent.php?confirmButton=Confirm
So, I would like to ask that it is my concept wrong or did I do it the wrong way?
Thanks for the help / advice... :)
Sorry for any inconvenience.
If you use method="GET", you can't put parameters in the action URL. You should use a hidden input field instead:
<form name="confirm" method="get" accept-charset="utf-8" action="confirm_sent.php">
<input type="hidden" name="name" value="?PHP echo $name; ?>">
But if you need to use multipart/form-data, because you have a file input, you can't use method="GET", you have to use method="POST". In that case, you can either have the parameter in the URL or in a hidden field.
<form name="confirm" method="post" accept-charset="utf-8" enctype="multipart/form-data" action="confirm_sent.php?name=<?PHP echo $name; ?>">
The name parameter will be in $_GET['name'], all the other inputs will be in $_POST (except the file inputs will be in $_FILES).

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

Categories