HTML post form with preset variables - php

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" />

Related

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

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

Query String in form does not work

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>

convert this link to POST mode

How can I convert this $_GET method to $_POST method?
$linkword .= "\n$alpha[$c] ";
You cannot pass $_POST data in a url, you must use cURL (PHP), AJAX (javascript), or a similar tool to build full HTTP requests.
ALTERNATE SOLUTION
You could however build a small form that submits, but you would have to use a submit button control for the "link" and use some hidden form inputs. You can Re-style the button anyway you wish with CSS.
<form action="link/url.php" method="post">
<input type="hidden" name="letters" value="value of letters" />
<input type="hidden" name="n" value="value of $n" />
<input type="submit" value="text of button" name="submit" />
</form>
You cannot POST data using URL Parameters. You will have to use a form with a method set to POST. Then submit the form on clicking the link or use a button.
Solution would be to use hidden form elements for your params and submit the hidden form when your click on the anchor tag.
Another solution would be to use ajax to post data. If you use jQuery or some libraries, you can do so.
Instead of creating a anchor tag. Create a hidden form
$linkword .= "\n$alpha[$c]";
Here is a Sample
<form name="hiddenform" method="POST" action="page.php">
<input type="hidden" name="letters" value="<? echo alpha[$c].$letters; ?>" />
<input type="hidden" name="n" value="<? echo $n; ?>" />
<a onclick="document.forms['hiddenform'].submit();">Test Link <? echo $alpha[$c]; ?></a>
</form>
Jsfiddle Demo

Categories