I am trying to get some data from page to page and then mail them.
So from one form I am getting a title of item :
//Form1
<form class="orderFormFields" method="post" action="order">
<input type="hidden" name="productName" value="<?php the_title(); ?>">
<input class="oButton" value="Order" type="submit"/>
</form>
And then is another form (next page) with other fields witch I need to mail :
<?php
//getting a variable from previous form
$product = $_POST['productName'];
if(isset($_POST['submit']))
{
$name = $_POST['order_name'];
$mail = $_POST['email'];
$phone = $_POST['mobile'];
$date = $_POST['date'];
$comment = $_POST['comment'];
//simple mail function goes here
$done = true;
}
?>
//Form2 goes here
So if I insert <?php echo $product; ?> before if(isset($_POST['submit'])) I can see my variable from previous page and all works just find. But when I am inserting that same variable in mail function witch is inside if(isset($_POST['submit'])) , I cant mail that variable, seems like it is empty.
Does form method POST delete all previous form data? Because, if I change my Form1 method to GET and $product = $_POST['productName']; to $product = $_GET['productName']; I am getting that variable after Form2 submit and I can mail that variable. But I would like to prefer using POST method, because of nice URL.
You forgot to name your submit button so there is no $_POST['submit']
<input class="oButton" value="Order" type="submit" name="submit" />
EDIT:
Okay, $_POST is array and have its values only after the post request. If you make another post request or change the page the previous values of $_POST are deleted and these from the new request are stored.
You can store data from the first post in the sessions for example -
$_SESSION['postData']['form1'] = $_POST;
Related
I have a php form
After filling fields I submit the form
After submitting form I have in the same window (a pop up) a print (via echo) of strings using variables values previously entered in the form
I need a way to go back to the form with fields filled by the values entered previously in order to modify or review them and then submit the form again
My form is created in a proprietary php framework. A field is so defined in a file named base.env:
$sf_vaccine['fields']['taxCode'] = new TextField('taxCode');
$sf_vaccine['fields']['taxCode']->label = 'Codice fiscale';
$sf_vaccine['fields']['taxCode']->addFlag(Field::NOT_EMPTY());
$sf_vaccine['sheet']->addField($sf_vaccine['fields']['taxCode']);
After a research on the web and differents attempts I did this. After submitting the form I print a link to the previous page:
echo "<center>Go Back</a></center></br></br></br>";
It allows me to back to the previous page but fields are empty and not filled as I need
I've also made an attempt using sessions: here what I did:
<?php
session_start();
echo "<center><a href=http://192.168.228.34/it/centre_rm/card_block/?_command=insert&prefill=yes><b>Go Back</b></a></center></br></br></br>";
$_GET['prefill']=='yes';
$_SESSION['taxCode'] = $record['taxCode'];
$fields['taxCode']->value = $_SESSION['taxCode'];
Don't use javascript history for this, but just go back to the page after submitting the form and add the values to the form.
Something like this:
<?php
$length = '';
$width = '';
if (!empty($_POST)) {
if (isset($_POST['length'])) {
$length = $_POST['length'];
}
if (isset($_POST['width'])) {
$width = $_POST['width'];
}
echo "Submitted length '$length' and width '$width'.";
}
?>
<form method="POST" action="">
Length: <input type="text" name="length" value="<?= $length ?>"/>
Width: <input type="text" name="width" value="<?= $width ?>"/>
<input type="submit">
</form>
I've added the echo-statement as sort of confirmation message but this can be removed if not needed.
Because of the action="" part, after clicking submit, the same page wil reload. De input values are sent and can be reused on the page reload.
Within the if-statements the submitted values are assigned to a variable to add these again in the input fields within the value tag so they are prefilled with the submitted data.
If you'd like to modify the submitted values before adding them to the inputs, you can do this within the if-statements and adjust the data accordingly.
I have four input texts and one submit button
enter image description here
what I want is for the results of the input text to turn into a link that I use as a whatsapp message like this.
https://wa.me/628123456789?text=Hai%20My%20Name%20Andi
what should i do?
this is my code
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<!-- https://wa.me/628123456789?text=textInput1%20textInput2%20textInput3%20textInput4 -->
<form action="https://wa.me/628123456789?text=textInput1%20textInput2%20textInput3%20textInput4" method="post">
<input type="text" name="textInput1" id="textInput1" ><br><br>
<input type="text" name="textInput2" id="textInput2" ><br><br>
<input type="text" name="textInput3" id="textInput3" ><br><br>
<input type="text" name="textInput4" id="textInput4" ><br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
//target url --> https://wa.me/628123456789?text=textInput1%20textInput2%20textInput3%20textInput4
$url = null;
if(isset($_POST['submit']))
{
$textInput1 = $_POST['textInput1'];
$textInput2 = $_POST['textInput2'];
$textInput3 = $_POST['textInput3'];
$textInput4 = $_POST['textInput4'];
$url = "https://wa.me/628123456789?text=".$textInput1." ".$textInput2." ".$textInput3." ".$textInput4;
}
?>
</body>
</html>
this is var_dump result for my code
enter image description here
Simply set your form to method post, see my code below. Add name attributes to your inputs fields and submit button so you can retrieve their values through http post. Then check to see if the submit button has been posted using isset($_POST['submit']). If isset, we assign the values of your input fields to variables in order to recreate the urls post key/value pairs.
IMPORTANT NOTE: I am not covering cleaning of your input fields make sure to read up on proper cleaning of inputs depending on what you are allowing to be processed by the back-end code in order to recreate your url.
<form action="/https://wa.me/628123456789?text=textInput1%20textInput2%20textInput3%20textInput4" method="post">
<input type="text" name="textInput1" id="textInput1" ><br><br>
<input type="text" name="textInput2" id="textInput2" ><br><br>
<input type="text" name="textInput3" id="textInput3" ><br><br>
<input type="text" name="textInput4" id="textInput4" ><br><br>
<input type="submit" name="submit" value="Submit">
</form>
PHP:
//target url --> https://wa.me/628123456789?text=textInput1%20textInput2%20textInput3%20textInput4
$url = null;
if(isset($_POST['submit'])){
$textInput1 = $_POST['textInput1'];
$textInput2 = $_POST['textInput2'];
$textInput3 = $_POST['textInput3'];
$textInput4 = $_POST['textInput4'];
$url = "https://wa.me/628123456789?text=".$textInput1." ".$textInput2." ".$textInput3." ".$textInput4;
}
Example OUTPUT. Simply outputting the url within an html page in example:
In your html simply echo out the variable $url. it will only display in your code when it is actually set. <?=$url?> or <?php echo $url;?>
NOTE: If you are trying to place the link into your action attribute of your form prior to submitting the page so that the values set in the input fields are a part of the forms action, you will need to get the values before submitting the page by using JS or JQuery and getting the values of the inputs on change or something of that nature, then build the url in js/jquery then set your forms attribute action using JS/Jquery.
EDIT: I want the result of the input text to be a whatsapp message, so it will be placed in the url link. I have updated my post
Okay, to redirect user once the form is filled out and you have sanitized your inputs, set the url in your a header() function and redirect your user to the desired url.
*Make sure you remove the action attribute from your form as you will be redirecting using the php header() function instead.
if(isset($_POST['submit'])){
// I am using filter_var(FILTER_SANITIZE_STRING) in this example.
$textInput1 = filter_var ( $_POST['textInput1'], FILTER_SANITIZE_STRING);
$textInput2 = filter_var ( $_POST['textInput2'], FILTER_SANITIZE_STRING);
$textInput3 = filter_var ( $_POST['textInput3'], FILTER_SANITIZE_STRING);
$textInput4 = filter_var ( $_POST['textInput4'], FILTER_SANITIZE_STRING);
// make sure to test this url by echoing it out before you run the header redirect.
$url = urlencode("https://wa.me/628123456789?text=".$textInput1." ".$textInput2." ".$textInput3." ".$textInput4);
header("Location: $url");
exit();
}
Using a conditional with a foreach loop with header to contruct url from post values:
if(isset($_POST['submit'])){
$url = "https://wa.me/628123456789?text="; // declare the core of your url without the post values
$i = 1; // increment
$k = 0; // key value for $inputs
$userinput = ''; // empty variable to hold user inputs for encoding
$num = count($_POST) - 1; // count the number of items in the array to properly format spaces in url string subtract one for submit button
foreach($_POST as $value){ // run a foreach loop on the $_POST
if($value !== "Submit"){ // we remove the submit post value from our array by omitting it using does not equal
$inputs[] = filter_var ( $value, FILTER_SANITIZE_STRING); // create a new array and push values into it
if($i < $num){ // all but last iterations will produce the space
$url .= $input[$k]." ";
}else{ // last iteration will not have a space
$url .= $input[$k];
}
}
}
$url .= urlencode($userinput);
echo $url; // for testing purposes to make sure the string is populating the input values as you have entered them delete this line after testing.
//header("Location: $url"); <-- Uncomment this line to redirect
//exit(); <-- uncomment exit() if you uncomment header() to close after redirect to make sure code stops on this page.
}
Just add the function urlencode() before the content:
$url = urlencode("https://wa.me/628123456789?text=".$textInput1." ".$textInput2." ".$textInput3." ".$textInput4);
I'm trying to make a simple form that is validated and errors should be shown. Also, the values of the fields should stay.
I'm using simple routing code to determine which page to show.
My problem is that the values of the form always reset when I submit it.
I googled a bit and found that when the Request changes, the form values get lost.
That's a small example that shows what I want to achieve:
$route = $_SERVER['REQUEST_URI'];
switch ($route) {
case '/kontakt':
?>
<form method="POST" action="/kontakt">
<input type="text" required name="test">
<input type="submit">
</form><?php
break;
}
After submitting the entered value should stay in the field.
So how can I keep the Request when routing to the same route but one time with POST and one time with GET without changing the form value to use the _POST array?
Lets first grab which request we need to use to get the request arguments.
$request =& $_SERVER['REQUEST_METHOD'] === 'POST' ? $_POST : $_GET;
It would probably be a good idea here to check it is set, if it isn't - just leave it blank.
$name = $request['name'] ?? ''; # PHP 7+
$name = isset($request['name']) ? $request['name'] : ''; # PHP 5.6 >
You can then do your routing
# switch: endswitch; for readability
switch(($route = $_SERVER['REQUEST_URI'])):
case '/kontack': ?>
<form method="POST" action="/kontakt">
<input type='text' value='<?= $name; ?>' name='name' />
....
<?php break;
endswitch;
This will then continuously insert the name back in to the value field. However, if you visit a new page and then come back - it will be gone. If you want it to stay at all times, through-out any route, you can use sessions.
session_start();
# We want to use the request name before we use the session in-case the user
# Used a different name to what we previously knew
$name = $request['name'] ?? $_SESSION['name'] ?? ''; # PHP 7
$name = isset($request['name']) ? $request['name'] : isset($_SESSION['name']) ? $_SESSION['name'] : ''; # PHP 5.6 >
# Update what we know
$_SESSION['name'] = $name;
Note: I showed both PHP 5.6> and PHP 7 examples. You only need to use one based on which PHP version you're using.
When you are getting to the route in the first time, then send a HTML-valueAttribute-variable as null. When you go back to the route after posting send the post value to the HTML-valueAttribute-variable:
When you reach the route the first time:
<?php
//Value that is sent to the view/page when accessing route without having posted a value
$testValue=null
?>
<form method="POST" action="/kontakt">
<input type="text" required name="test"
<?php
if($testValue != null)
{
echo "value='".$testValue."'";
}
?>
>
<input type="submit">
</form>
When you use the route after have posted:
<?php
//Value that was posted is sent to view/page
$testValue=$POST['test']
?>
<form method="POST" action="/kontakt">
<input type="text" required name="test"
<?php
if($testValue != null)
{
echo "value='".$testValue."'";
}
?>
>
<input type="submit">
</form>
I am having bad time getting SESSION to work in PHP.
I have a form the following action:
<form action="confirm.php" method="post">
And that has a button as such:
<button type="submit" id="submit">Proceed</button>
I have got session_start(); on all my pages. After the form button, I have this code:
<?php
if(!empty($_POST['submit']))
{
$_SESSION['name'] = $_POST['name'];
$_SESSION['address'] = $_POST['address'];
$_SESSION['strtnum'] = $_POST['strtnum'];
$_SESSION['height'] = $_POST['height'];
}
?>
On confirm.php I've got this:
<?php
print_r($_SESSION);
print_r($_POST);
?>
The POST array has correct values but the SESSION array is completely empty with no variables or values at all.
I would like help understanding how fix this.
Thank you.
EDIT: I'm quite sure that the code doesn't actually reach the inside of the if statement. I added an echo in there to print an alert (yes I used ) and it doesnt work. So I'm not 100% sure that it enters the if(!empty($_POST['submit']))
<form action="confirm.php" method="post">
^^^^^^^^^^^^^^^^^^^^
Your form is being submitted to confirm.php, thats where you should handle the $_POST and fill the $_SESSION values. Instead you are trying to do it in the same page that prints the form, but thats doing no good there because its not where the form data is being submitted to.
How do I maintain the $post value when a page is refreshed; In other words how do I refresh the page without losing the Post value
This in not possible without a page submit in the first place! Unless you somehow submitted the form fields back to the server i.e. Without Page Refresh using jQuery etc. Somesort of Auto Save Form script.
If this is for validation checks no need for sessions as suggested.
User fills in the form and submits back to self
Sever side validation fails
$_GET
<input type="hidden" name="first"
value="<?php echo htmlspecialchars($first, ENT_QUOTES); ?>" />
validation message, end.
alternatively as suggested save the whole post in a session, something like this, but again has to be first submitted to work....
$_POST
if(isset($_POST) & count($_POST)) { $_SESSION['post'] = $_POST; }
if(isset($_SESSION['post']) && count($_SESSION['post'])) { $_POST = $_SESSION['post']; }
You can't do this. POST variables may not be re-sent, if they are, the browser usually does this when the user refreshes the page.
The POST variable will never be re-set if the user clicks a link to another page instead of refreshing.
If $post is a normal variable, then it will never be saved.
If you need to save something, you need to use cookies. $_SESSION is an implementation of cookies. Cookies are data that is stored on the user's browser, and are re-sent with every request.
Reference: http://php.net/manual/en/reserved.variables.session.php
The $_SESSION variable is just an associative array, so to use it, simply do something like:
$_SESSION['foo'] = $bar
You could save your $_POST values inside of $_SESSION's
Save your all $_POST's like this:
<?php
session_start();
$_SESSION['value1'] = $_POST['value1'];
$_SESSION['value2'] = $_POST['value2'];
// ETC...
echo "<input type='text' name='value1' value='".$_SESSION['value1']."' />";
echo "<input type='text' name='value2' value='".$_SESSION['value2']."' />";
?>
Actually in html forms it keeps post data.
this is valuble when you need to keep inserted data in the textboxes.
<form>
<input type="text" name="student_name" value="<?php echo
isset($_POST['student_name']) ? $_POST['student_name']:'';
?>">
</form>
put post values to session
session_start();
$_SESSION["POST_VARS"]=$_POST;
and you can fetch this value in another page like
session_start();
$_SESSION["POST_VARS"]["name"];
$_SESSION["POST_VARS"]["address"];
You can use the same value that you got in the POST inside the form, this way, when you submit it - it'll stay there.
An little example:
<?php
$var = mysql_real_escape_string($_POST['var']);
?>
<form id="1" name="1" action="/" method="post">
<input type="text" value="<?php print $var;?>"/>
<input type="submit" value="Submit" />
</form>
You can use file to save post data so the data will not will not be removed until someone remove the file and of-course you can modify the file easily
if($_POST['name'])
{
$file = fopen('poststored.txt','wb');
fwrite($file,''.$_POST['value'].'');
fclose($file);
}
if (file_exists('poststored.txt')) {
$file = fopen('ipSelected.txt', 'r');
$value = fgets($file);
fclose($file);
}
so your post value stored in $value.