PHP recover data from Post - php

I have inside loop some fields for send inside form with other fields :
<?php
for($f=1;$f<=3;$f++)
{
?>
<input type="text" name="friend['email_<?php echo $f;?>']" value="<?php echo $_POST['friend']['email_'.$f.''];?>">
<?php
}
?>
When i send from form i need get the value of each field if no empty , i think the problem it´s in no recover the value send from the form , i don´t know if i writte right
Ony it´s that problem , thank´s , the best regards

I think the problem here is that you have single quotes around email_<?php echo $f; ?> - in POST data, these are not needed. So, you would have:
<form action="myPage.php" method="POST">
<?php for ($f=1;$f<=3;$f++) { ?>
<input type="text" name="friend[email_<?php echo $f; ?>]" value="<?php echo ... ?>">
<?php } ?>
<input type="submit">
</form>
PHP will parse POST data into associative arrays, so you will get back a 'friend' object in $_POST. You can access this like so:
<?php
$friends = isset($_POST['friend']) ? $_POST['friend'] : array();
print_r($friends);
?>

I'm not sure if i'm getting it right. You must send your form data in order to manipulate it. For example if you want to print all the form data:
Client Side:
<form action="boo.php" method="POST">
<input type="text" name="name">
<input type="text" name="age">
<input type="submit">
</form>
Server Side:
<?php
$name = "";
$age = "";
if( isset($_POST['name']) && isset($_POST['age']) )
{
$name = htmlspecialchars($_POST['name']);
$age = htmlspecialchars($_POST['age']);
}
echo 'Your name is ' . $name . ' and your age is ' . $age;
?>
If you are trying to retain the values even after invalid input you can just echo the input in value attribute of the input field.

Related

PHP: store html form input into php variable

I like to store an input of a HTML form into a PHP variable:
HTML Form:
<form action="shell.php" method="post">
<p>Youtube Link: <input type="text" name="url" /></p>
<p><input type="submit" /></p>
</form>
PHP File (shell.php)
<?php
value=<?php echo ($_POST['url']); ?>;
exec('/bin/bash /home/d_youtube/youtube.sh "$value"');
?>
How I can store the input of the HTML form into the php variable $value?
Thanks for your help, best regards
You don't need to open another PHP tag. Replace your code to below:
<?php
$value = $_POST['url'];
exec('/bin/bash /home/d_youtube/youtube.sh "$value"');
?>
And if you want to verify the value is not empty:
<?php
if (isset($_POST['url'])) {
$value = $_POST['url'];
exec('/bin/bash /home/d_youtube/youtube.sh "$value"');
}
?>
You don't need to modify the file itself. You just can pass the input VALUE to the required expression:
Check this:
<?php
if( !empty($_POST['url']) )
exec('/bin/bash /home/d_youtube/youtube.sh "' . $_POST['url'] . '"');
?>

How to Automatically POST Form if Variable is passed via URL

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 */
}

How to get input field value using PHP

I have a input field as follows:
<input type="text" name="subject" id="subject" value="Car Loan">
I would like to get the input fields value Car Loan and assign it to a session. How do I do this using PHP or jQuery?
Use PHP's $_POST or $_GET superglobals to retrieve the value of the input tag via the name of the HTML tag.
For Example, change the method in your form and then echo out the value by the name of the input:
Using $_GET method:
<form name="form" action="" method="get">
<input type="text" name="subject" id="subject" value="Car Loan">
</form>
To show the value:
<?php echo $_GET['subject']; ?>
Using $_POST method:
<form name="form" action="" method="post">
<input type="text" name="subject" id="subject" value="Car Loan">
</form>
To show the value:
<?php echo $_POST['subject']; ?>
Example of using PHP to get a value from a form:
Put this in foobar.php:
<html>
<body>
<form action="foobar_submit.php" method="post">
<input name="my_html_input_tag" value="PILLS HERE"/>
<input type="submit" name="my_form_submit_button"
value="Click here for penguins"/>
</form>
</body>
</html>
Read the above code so you understand what it is doing:
"foobar.php is an HTML document containing an HTML form. When the user presses the submit button inside the form, the form's action property is run: foobar_submit.php. The form will be submitted as a POST request. Inside the form is an input tag with the name "my_html_input_tag". It's default value is "PILLS HERE". That causes a text box to appear with text: 'PILLS HERE' on the browser. To the right is a submit button, when you click it, the browser url changes to foobar_submit.php and the below code is run.
Put this code in foobar_submit.php in the same directory as foobar.php:
<?php
echo $_POST['my_html_input_tag'];
echo "<br><br>";
print_r($_POST);
?>
Read the above code so you know what its doing:
The HTML form from above populated the $_POST superglobal with key/value pairs representing the html elements inside the form. The echo prints out the value by key: 'my_html_input_tag'. If the key is found, which it is, its value is returned: "PILLS HERE".
Then print_r prints out all the keys and values from $_POST so you can peek as to what else is in there.
The value of the input tag with name=my_html_input_tag was put into the $_POST and you retrieved it inside another PHP file.
You can get the value $value as :
$value = $_POST['subject'];
or:
$value = $_GET['subject']; ,depending upon the form method used.
session_start();
$_SESSION['subject'] = $value;
the value is assigned to session variable subject.
For global use, you may use:
$val = $_REQUEST['subject'];
and to add yo your session, simply
session_start();
$_SESSION['subject'] = $val;
And you dont need jQuery in this case.
function get_input_tags($html)
{
$post_data = array();
// a new dom object
$dom = new DomDocument;
//load the html into the object
$dom->loadHTML($html);
//discard white space
$dom->preserveWhiteSpace = false;
//all input tags as a list
$input_tags = $dom->getElementsByTagName('input');
//get all rows from the table
for ($i = 0; $i < $input_tags->length; $i++)
{
if( is_object($input_tags->item($i)) )
{
$name = $value = '';
$name_o = $input_tags->item($i)->attributes->getNamedItem('name');
if(is_object($name_o))
{
$name = $name_o->value;
$value_o = $input_tags->item($i)->attributes->getNamedItem('value');
if(is_object($value_o))
{
$value = $input_tags->item($i)->attributes->getNamedItem('value')->value;
}
$post_data[$name] = $value;
}
}
}
return $post_data;
}
error_reporting(~E_WARNING);
$html = file_get_contents("https://accounts.google.com/ServiceLoginAuth");
print_r(get_input_tags($html));
If its a get request use, $_GET['subject'] or if its a post request use, $_POST['subject']
<form action="" method="post">
<input type="text" name="subject" id="subject" value="Car Loan">
<button type="submit" name="ok">OK</button>
</form>
<?php
if(isset($_POST['ok'])){
echo $_POST['subject'];
}
?>

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

Open page with Form pre-filled

I have an entry Form. When the page is loaded, it must check:
if ($_SESSION[WorkMode] == 'UPDATE')
Then fill the Form with values from the database, else open a blank Form.
If I fetch the results in a different PHP file and call this .php file on load, how to fill the Form.
Set the variables that hold the values for your form, then include the "template" of the form you're having.
File 1:
<?php
$res = mysql_query("..");
if($res) {
$row = mysql_fetch_assoc($res);
$name = $row['name'];
$birthday = $row['birthday'];
...
include('form.tpl');
}
File 2 (form.tpl)
<form action="">
<input type="text" name="username" value="<?php isset($name)?$name:""; ?>" />
.. and so on
</form>
Alternatetively you can use a full blown template engine like Smarty to do the job for you.
Best wishes,
Fabian
$formvalue = NULL;
if ($_SESSION[WorkMode] == 'UPDATE')
$formvalue = $some_database_value;
echo '<input type="text" name="myname" value="'.$formvalue .'" />';
I found out that the following did it for me, adding the variable after a column.
<label>Firstname : </label><input type="text" name="Firstname" value= "<?php echo (isset($_POST['firstname'])) ? htmlspecialchars($_POST['firstname']) : $firstname;?>"

Categories