Using PHP to pass a form field as URL parameter - php

I have a form field:
<input type="text" value="" name="Email" id="Email">
and my form action confirmation url is this:
http://.../index.php?Email=<?php echo $_POST['Email']; ?>
However after submitting, the Email parameter is not coming through. Is this something that can be done in PHP, or does it only read the field on initial page load?
Thanks

Your issue is that you are mixing $_GET and $_POST.
See your code here, http://.../index.php?Email=<?php echo $_POST['Email']; ?>, when you post to that one, there will no longer be a $_POST['Email'], but a $_GET['Email']. So the first post will likely work (if you are using <form method="post" action="...">), but the second submit will fail, as $_POST['Email'] no longer exists.
So I recommend that you don't use parameters in the action. Instead, put them in a hidden field or switch to only $_GET parameters.
Option 1, use hidden field
Change the form on your second page to:
<form action="http://.../index.php" method="POST">
<input type="hidden" name="Email" id="Email" value="<?php echo $_POST['Email'];?>" />
...
</form>
Option 2, use only $_GET
Change the form on your first page to <form ... method="GET">
And then change the form on the second page to use $_GET['Email'] and the method to GET.
<form action="http://.../index.php??Email=<?php echo $_GET['Email'];?>" method="GET">
...
</form>
Option 3, Use $_REQUEST instead of $_POST
Simply use http://.../index.php?Email=<?php echo $_REQUEST['Email']; ?> as your action url, as $_REQUEST is a merge of $_GET and $_POST. Be aware that this is a merge of $_GET, $_POST and $_COOKIE.

It depends on the your FORM method
Your form should be
<form method='post' action='http://.../index.php'>
<input type="text" value="" name="Email" id="Email">
<input type='submit' value='Post data'>
</form>
and to access email in index.php you can write code as below
<?php
$emailValue = $_POST["Email"];
//Use variable for further processing
?>
if your form is as below (please check that method is get
<form method='get' action='http://.../index.php'>
<input type="text" value="" name="Email" id="Email">
<input type='submit' value='Post data'>
</form>
and to access email in index.php you can write code as below
<?php
$emailValue = $_GET["Email"];
//Use variable for further processing
?>

You don't need to define the structure of the GET request; that's what the form does.
For instance:
<form action="workerbee.php" method="GET">
<input type="text" name="honey_type" value="sweet" />
</form>
...when submitted, would automatically append the field - honey_type - to the URL for you. It would end up like this:
http://example.com/workerbee.php?honey_type=sweet
You can then access the value via $_GET['honey_type'] in workerbee.php. To pre-fill the form with the existing submitted value - assuming that workerbee.php holds the form - just add a conditional value parameter:
<?php
$honey_type = !empty($_GET['honey_type']) ? $_GET['honey_type'] : null;
?>
<input type="text" name="honey_type" value="'<?php echo htmlspecialchars($honey_type); ?>'" />

If you're trying to use data from your current form, your form tag should read as follows:
<form action="http://.../index.php" method="GET">
If you're trying to pass data your server already has (such as from a previous form), then you should use a hidden field instead:
<input name="email" type="hidden" value="<?php echo $_POST['Email']; ?>">

Related

$_POST doesn't work with my form

Now i made a simple form with 2 input one for name and one for button
i want in php file echo name input
<form action="del.php">
<input type="text" name="name" />
<input type="submit" name="btn" value="button"/>
</form>
in del.php
<?php
session_start();
$_SESSION['name'] = $_POST['name'];
echo "".$_POST['name']."";
giving me white screen when every time
i want to echo any thing that typed in this input
You are missing method attribute in your form.
<form action="del.php" method="POST">
<input type="text" name="name" />
<input type="submit" name="btn" value="button"/>
</form>
The method attribute specifies how to send form-data (the form-data is sent to the page specified in the action attribute).
The form-data can be sent as URL variables (with method="get") or as HTTP post transaction (with method="post").
Source: https://www.w3schools.com/tags/att_form_method.asp
the default method when a form is submitted is GET, you need to specify the POST method (method="post") in your <form> tag:
<form action="del.php" method="post">
Your form is not POSTing its GETing (see the url params which will be set example.com?name=) this is because you not set the from method for POST:
<form action="del.php" method="post">
<input type="text" name="name" />
<input type="submit" name="btn" value="button"/>
</form>
And within your PHP you should check its post and check that the values are set:
<?php
session_start();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = isset($_POST['name']) ? $_POST['name'] : null;
// set your session - not sure you need this :/
$_SESSION['name'] = $name;
// echo out your result but also protect from XSS by using htmlentities
echo htmlentities($name);
}

Fetch value of a input type and store in variable

I have following input type
<input id="crp-project-title" class="crp-project-title" type="text" placeholder="Enter project title" value="EXTERIOR" name="project.title">
Its value is value="EXTERIOR". I want to fetch the value and store in a variable. I am not getting any idea regarding this. I am trying to modify plugin (career portfolio) according to my project and its part of that.
Your html form:
<form method="POST" action="process.php">
<input type="text" name="foo">
<input type="submit">
</form>
Your Php form processing page (process.php):
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$foo = filter_input(INPUT_POST, 'foo', FILTER_SANITIZE_STRING);
}
?>
This is front end code, if you are trying to move front end data to server side it must pass through the server. i.e. $_POST...
now you can either do this with AJAX / PHP or PHP / HTML only
<?php
if(isset($_POST[project.title])
$var = $_POST[project.title]; //its now in a varible
?>
<form method="POST" action="thispage.php">
<input id="crp-project-title" class="crp-project-title" type="text" placeholder="Enter project title" value="EXTERIOR" name="project.title">
<input type=submit value="PRESS ME TO SUBMIT VALUE TO VAR">
</form>

How use $_GET variable in a Wordpress page

I have a form for asking information about courses , every course has it page, but the information page is one for all.
The form should be something like that:
<form action="#" method="POST">
<label for="name">Name</label>
<input name="name" type="text">
<label for="email">Email</label>
<input name="email" type="email">
<input type="hidden" id="code" value="<?php echo $course_code; ?>">
<input id="submit" type="submit" value="Invia" />
</form>
I wish to change the var $course code according to the referrer page. (With a $_GET var)
I tried "Shortcode Exec PHP" plugin to execute php in wp pages, but doesnt work.
When you POST the form, the variable won't be set in $_GET but in $_POST. It's either one or the other, so if you want to read the $_GET var, you must also use GET on the form, like this:
<form action="#" method="GET">
<label for="name">Name</label>
...
(this is what Fred commented on, but I couldn't expand upon that comment due to my low rep)
I was wrong to use "Shortcode Exec PHP" plugin.
I set a shortcode:
$course_name = $_GET['cn'];
$courses= array("courses1","courses2","couses3");
if (in_array($course_name, $courses)) {
echo $course_name:
}
and the in the wordpress page can be used the name of the shortcode
[couse_name]
Now its work!
You can just use $_REQUEST so it doesn't matter if its a POST or a GET from the form. But I wouldn't use GET from a form unless it was a search or something where the user could bookmark the url and see the result. Mostly use POST for all other instances.
HTML form...
<form method="post">
<label>Name<br>
<input type="text" name="name">
</label>
...
<input type="submit" value="Invia">
</form>
PHP page that handles the form...
<?php
// $_REQUEST will contain POST, GET & COOKIE
echo $_REQUEST['name'];
?>

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

how can i use the PHP method $_GET[] with the same variable name in different php files

In index.php, I have created a form and in the action attribute of the form tag I have specified "page2.php".
page2.php is also a form, (which continues on from index.php) and in the action attribute of the form tage I have specified "page3.php". I can retrieve what the user entered into the "location" text box, in the form on index.php and display it in page2.php through the method
<?php echo $_GET["location"]; ?>
but, I now also want to display the user location in page3.php, but when I use the above method it does not work. It give me the error: "Undefined index: location". I take it is because page3.php can not access the fields from index.php, but how do I get this to work?
Thanks in advance
You will need to store the value of $_GET["location"]; in a hidden field on page2.php within the form that gets submitted to page3.php.
Update Example
page2.php
<form action="page3.php">
<input type="hidden" value="<?php print $_GET['location']; ?>" name="location_from_page2" />
page3.php
$location = $_GET["location_from_page2"];
What you're doing here is printing out the contents of location into the value of a hidden field and then you're reading this hidden field in page3.php
This is one way to do it but you might want to consider looking into the php sessions.
Try using session variables
$_SESSION['location'] = $_GET['location'];
You can use hidden fields to achieve this.
page2.php:
<form id="myform" action="/page3.php" method="get">
<fieldset>
<label>x</label> <input type="text" name="something" value=""><br><br>
<input type="hidden" name="location" value="<?php echo $_GET['location']; ?>">
<input type="submit" name="submit" value="submit">
</fieldset>
</form>
You need to include the values in your form on the 2nd page.
Like: <input type="hidden" name="location" value="<?= $_GET["location"]; ?>">
or append the variable to your action in the form.
Otherwise you could just use Sessions - which would probably be easiest.

Categories