How to Automatically POST Form if Variable is passed via URL - php

I want form to post automatically if zip variable is passed from URL.
URL looks like: www.sitename.com/maps/zipsearch.php?zip=90210
Form looks like:
<form method="post">
Zipcode:
<input name="zip" value="<?php echo (isset($_GET["zip"]))? $_GET["zip"]:"";?>" />
<input type="submit" name="subbut" value="Find instructors" />
</form>
So it fills the input box with zip code but I would like it to post automatically to see results again if zip is passed.
Maybe an IF / THEN?
Any help would be appreciated.

You mean to echo the value passed in GET parameter?
<input type="submit" name="subbut" value="<?php echo isset($_GET['zip'])?$_GET['zip']:'Find'; ?>" />
EDIT
Or, if you are asking about submitting the form, then something like this might work I believe:
<input type="submit" name="subbut" value="<?php echo isset($_GET['zip'])?$_GET['zip']:'Find'; ?>" />
<?php if( isset( $_GET['zip'] ) ) { ?>
<script>
document.forms["name_of_the_form_here"].submit();
</script>
<?php } ?>

like this:
<form id="form" action="form.php" method="post">
Zipcode:
<input name="zip" value="<?php echo (isset($_GET["zip"]))? $_GET["zip"]:"";?>" />
<input type="submit" name="subbut" value="Find instructors" />
</form>
<?php if (isset($_GET["zip"])): ?>
<script>document.getElementById('form').submit()</script>
<?php endif; ?>

since passing data via URL means GET method, so i think you have a little misconception with your question.
if you would like to post automatically you dont need to show form.
just put this code in your zipsearch.php
if ($_GET['zip'] != ""){
// do what you want if zip parameter is not null
}else{
// do what you want if zip parameter is null
}

It looks like your form is submitting to itself. (Eg. zipsearch.php displays HTML form. When user submits form, it is posted back to zipsearch.php which displays the search results).
If this is the case, you don't have to post anything, because you are already inside the file that handles the form submission. You could do something like this:
<?php
if (isset ($_POST['zip'])) {
$zip = $_POST['zip']; /* Form was submitted */
} else if (isset ($_GET['zip'])) {
$zip = $_GET['zip']; /* "?zip=" parameter exists */
}
if (isset ($zip)) {
/* Display search results */
} else {
/* Display form */
}

Related

Send form data to the same page not functionning

I'm a newbie in PHP, and I would like to send datas from a form and display it into the same page, here is my code for better understanding:
<form method="post" action="same_page.php">
<input type="text" name="owner" />
<input type="submit" value="Validate" />
</form>
<?php
if(isset($_GET['owner']))
{
echo "data sent !";
}
?>
So normally, after having entered some random text in the form and click "validate", the message "data sent!" Should be displayed on the page. I guess I missed something, but I can't figure out what.
You forgot to add submit name in your form.You are using POST as method so code should be
<form method="post" action="">
<input type="text" name="owner" />
<input type="submit" name="submit_value" value="Validate" />
</form>
<?php
if(isset($_POST['submit_value']))
{
echo '<pre>';
print_r($_POST);
}
?>
Will display your post values
You are using a POST method in your form.
<form method="post" action="same_page.php">
So, change your code to:
if (count($_POST) && isset($_POST['owner']))
Technically, the above code does the following:
First checks if there are content in POST.
Then, it checks if the owner is set.
If both the conditions are satisfied, it displays the message.
You can actually get rid of action="same_page.php" as if you omit it, you will post to the same page.
Note: This is a worst method of programming, which you need to change.
You should Replace $_GET['owner'] with $_POST['owner'] as in your form you have specified method='post'
Replace:
$_GET['owner']
With:
$_POST['owner']
Since you are using the post method in your form, you have to check against the $_POST array in your PHP code.

PHP $_POST give empty value

I don't know what happened but there's no value return.
Please check if i made any mistake
Value is blank if you have no check for null or blank value on transfer.php
Case 1 : You are access file in same flow then no need to modify any thing its wokring.If you are access file without press submit button.directly using url then post is blank.
e.g. http://localhost:8080/stackoverflow/transfer.php
Case 2 : MIME type issue, enctype="text/plain" in the form, this should fix the issue.
from_post.php
<html>
<body>
<form action="transfer.php" method="post" enctype="text/plain">
<input type="text" name="name" value="">
<input type="submit" name="submit" value="submit">
</form>
transfer.php
Here you want to check is that submit button if fired or not.
<?php
if(isset($_POST['submit']))
{
$u=$_POST['name'];
if(isset($u) || (!is_empty())){
echo $u;
}
else
{
echo "the post doesn't have any value";
}
echo "<br/>";
}
?>

Saving form values with php and calling cookie using SESSION

I am making a form in html. When a person clicks on submit, it checks if certain fields are filled correctly, so pretty simple form so far.
However, i want to save the text which is typed into the fields, if a person refreshes the page. So if the page is refreshed, the text is still in the fields.
I am trying to achieve this using php and a cookie.
// Cookie
$saved_info = array();
$saved_infos = isset($_COOKIE['offer_saved_info']) ? explode('][',
$_COOKIE['offer_saved_info']) : array();
foreach($saved_infos as $info)
{
$info_ = trim($info, '[]');
$parts = explode('|', $info_);
$saved_info[$parts[0]] = $parts[1];
}
if(isset($_SESSION['webhipster_ask']['headline']))
$saved_info['headline'] = $_SESSION['webhipster_ask']['headline'];
// End Cookie
and now for the form input field:
<div id="headlineinput"><input type="text" id="headline"
value="<?php echo isset($_SESSION['webhipster_ask']['headline']) ?
$_SESSION['webhipster_ask'] ['headline'] : ''; ?>"
tabindex="1" size="20" name="headline" /></div>
I am new at using SESSION within php, so my quesiton is:
Is there a simpler way of achieving this without using a cookie like above?
Or what have i done wrong in the above mentioned code?
First thing is I'm pretty sure you're echo should have round brackets around it like:
echo (isset($_SESSION['webhipster_ask']['headline']) ? value : value)
That's not really the only question your asking though I think.
If you're submitting the data via a form, why not validate using the form values, and use the form values in your html input value. I would only store them to my session once I had validated the data and moved on.
For example:
<?php
session_start();
$errors=array();
if($_POST['doSubmit']=='yes')
{
//validate all $_POST values
if(!empty($_POST['headline']))
{
$errors[]="Your headline is empty";
}
if(!empty($_POST['something_else']))
{
$errors[]="Your other field is empty";
}
if(empty($errors))
{
//everything is validated
$_SESSION['form_values']=$_POST; //put your entire validated post array into a session, you could do this another way, just for simplicity sake here
header("Location: wherever.php");
}
}
if(!empty($errors))
{
foreach($errors as $val)
{
echo "<div style='color: red;'>".$val."</div>";
}
}
?>
<!-- This form submits to its own page //-->
<form name="whatever" id="whatever" method="post">
<input type="hidden" name="doSubmit" id="doSubmit" value="yes" />
<div id="headlineinput">
<input type="text" id="headline" value="<?php echo $_POST['headline'];?>" tabindex="1" size="20" name="headline" />
<!-- the line above does not need an isset, because if it is not set, it will simply not have anything in it //-->
</div>
<input type="submit" value="submit" />
</form>

how to unset post array?

every time i am refreshing the page and i am getting the same value stored in the post array.
i want execution of echo statement only after submit and after refreshing no echo results..
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
echo "User name : <b> $name </b>";
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="name"><br>
<input type="submit" name="submit" value="Submit Form"><br>
</form>
From just a form, you won't be able to check if it was a refresh, or a first submit, regardless of using GET or POST method.
To ensure a single message, you need to:
a. redirect the user to somewhere else after you processed the request.
if(isset($_POST['submit'])) {
// process data
header("Location: new-url");
}
And display the message on the other URL.
b. set a cookie / session variable, which tells you the form was already processed.
if(isset($_POST['submit']) && !isset($_SESSION['form_processed'])) {
$_SESSION['form_processed'] = true;
}
This second approach will kill your form until the user closes the browser, so you should do something more complex - like storing another hidden field in the form, and storing that in the session.
If you submit a form and then refresh the resulting page, the browser will re-post the form (usually prompts first). That is why the POST data is always present.
An option would be to store a session variable and have it sent in the form, then check if it matches in the form processing code - to determine if it is a re-post or not.
Within the form:
<input type="hidden" name="time" value="<?php echo $time; ?>" />
In the PHP:
session_start();
if(isset($_POST['submit']))
{
if(isset($_SESSION['time']) && $_SESSION['time'] == $_POST['time'])
{
echo "User name : <b> $name </b>";
}
}
$time = $_SESSION['time'] = time();
Another option is to redirect after processing the post data:
if(isset($_POST['submit']))
{
...
...
header('Location: ' . basename($_SERVER['PHP_SELF']));
exit();
}
You need to maintain a state as to whether $name has already been displayed or not. The easiest way is probably to maintain that state in a browser cookie.
<?php
$nonce = $_COOKIE['nonce'];
$new_nonce = mt_rand();
setcookie('nonce', $new_nonce);
if(isset($_POST['submit']) && $_POST['nonce'] == $nonce)
{
$name = $_POST['name'];
echo "User name : <b> $name </b>";
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="nonce" value="<?php echo $new_nonce ?>">
<input type="text" name="name"><br>
<input type="submit" name="submit" value="Submit Form"><br>
</form>
Problems
you are polluting the user “session” with stale variable.
this will break if your user opens several windows (or tabs) to the same page. To fix this you would have to change the nonce cookie into an array of nonces, and update it accordingly.
if you want refresh page after submit use
<form method="get"
sure if your form hasn't a lot of data and also need to use $_GET instead of $_POST variable:)
correct way for you, but this logic is not good, need to refactor this script:
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
echo "User name : <b> $name </b>";
unset($_POST['submit']);
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="name"><br>
<input type="submit" name="submit" value="Submit Form"><br>
</form>

PHP sending text to a <input type="text">

How would I send information from a form to a block of PHP code and then back to a text area? I can not find this answer.
To print out the text you entered in the textfield on the next request would look like this assuming you render the same page (i.e. myform.php):
<?php
$fieldValue = htmlentities($_POST['myfield']);
?>
<form action="myform.php" method="post">
<label for="myfield">Your textfield:</label>
<input type="text" name="myfield" id="myfield" value="<?php echo $fieldValue; ?>" />
</form>
A very basic example.
Assuming you have a PHP file named index.php
<?php
$val = 'Nothing in POST';
if (!empty($_POST)) { //$_POST is where stuff posted from the FORM is saved
$val = isset($_POST['text']) ? $_POST['text'] : '';//you're looking for the data with key text which is the name of your textarea element
$val = 'Got something from POST : ' . $val;
}
?>
<form action='index.php' method='post'>
<textarea name='text'><?php echo $val ?></textarea>
</form>
Have a look at this tutorial for the basics : http://net.tutsplus.com/articles/news/diving-into-php/
use ajax!(and use jquery for that ajax!)
suppose you have this html :
<input type="text" id="input">
<textarea id="result"></textarea>
than the script should be:
$('#input').keypress(function(e){
if(e.wich != 13)//not enter
return;
$.get(your_php_file.php,{param1:val1,param2:val2},function(result){
$('#result').val(result);
});
});
when you hit enter on the input field,the ajax function is being called that requests the file your_php_file.php?param1=val1&param2=val2. With the php's result, the callback function is being called which updates your textarea

Categories