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);
Related
I am trying to get the value of a form (text field) with _POST, and store it to a text file but it doesn't work. Here's my code:
HTML
<form>
<input type="text" name="test" id="test" value="">
<input type="button" onclick="location.href='example.com/index.php?address=true';" value="ODOSLAŤ" />
</form>
PHP
if (isset($_GET['address'])) {
$email = $_POST['test'];
$myfile = fopen("log.txt","a") or die("Error.");
fwrite($myfile, "\n".$email);
// this prints nothing
echo $email;
}
I can't get the value of that text field. Nor GET nor POST doesn't work for me. What am I doing wrong?
You are missing a post method in your form.
<form method="post" action="example.com/index.php?address=true">
<input type="text" name="test" id="test" value="">
<input type="submit" value="ODOSLAŤ" />
</form>
You also have to change the following line.
if (isset($_GET['address'])) {
With
if (isset($_POST['address'])) {
You want to post after triggering an action as FreedomPride mentioned
Conclusion
You want to declare for example a post methodif you know that you would like to post that data in the future.
As FreedomPride also mentioned :
You are using a GET, but if a user does not input anything your script won't work, there by it is recommended to use a POST
You are not submitting your form.
Change your code as follows....
<form method="post" action="example.com/index.php?address=true">
<input type="text" name="test" id="test" value="">
<input type="submit" value="ODOSLAŤ" />
</form>
You'll get what you want....
This is what you're missing as Tomm mentioned.
<form method="post" action="yourphp.php">
<input type="text" name="address" id="test" value="">
<input type="submit" name="submit"/>
</form>
In your PHP, it should be post if it was triggered
if (isset($_POST['address'])) {
$email = $_POST['test'];
$myfile = fopen("log.txt","a") or die("Error.");
fwrite($myfile, "\n".$email);
// this prints nothing
echo $email;
}
Explanation :-
In a form, an action is required to pass the action to the next caller with action. The action could be empty or pass it's value to another script.
In your PHP you're actually using a GET. What if the user didn't input anything. That's why it's recommended to use POST .
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" />
My question is how do I call input value of user selected which has show by php code from another php.
Normal simple way we get the input this way
$calendar_id_val = $_GET['calendar_id'];
and now it is not working:
For example Show.php, I have one form which show the values from Database and show the result with php variable with While Loop.
<input name="calendar_id" value="<?php echo $calendar_id;?>">
and when user is submit that form I will carry these user selected value and perform insert.php
While you are doing echo in the input use echo $calendar_id_val
You should use form like,
<form action="insert.php" method="get">
<input type="text" name="calendar_id" value="<?php echo $calendar_id;?>"/>
<input type="submit" name="submit_id" value="Insert"/>
</form>
Insert.php
echo $calendar_id_val = $_GET['calendar_id'];
Does this answer your question?
<form action="insert.php" method="GET">
<input name="calendar_id" value="<?php echo $calendar_id;?>">
</form>
If you want to redirect the user back to show.php, add this to the end of your insert.php script
header('Location: show.php');
exit();
I suggest $_POST var like this:
<form action="insert.php" method="POST">
<input type="text" name="calendar_id" value="<?php echo $calendar_id;?>" />
<input type="submit" name="submit" value="Submit" />
</form>
insert.php file:
echo $calendar_id = $_POST['calendar_id'];
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>
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.