I have a form with quite a lot of input fields and checkboxes.
When I submit the form I want to write the form field name and value to a text file.
<input type="text" name="mailHost" value=""/>
<input type="text" name="mailUser" value=""/>
<input type="text" name="mailPass" value=""/>
So with this as an example it would be written to a file as :
mailHost = VALUE
mailUser = VALUE
mailPass = VALUE
For a few form fields it fine doing each on by hand, but is there a function or way to do this for numerous fields ?
And then the same for reading it back ?
Again using the same example above I'd end up with the following when read back :
$mailHost = Value, $mailUser = value etc where the variable name is dynamically created and the value assigned ?
Thanks
You can iterate through your $_POST array:
foreach ($_POST as $key => $value) {
file_put_contents('file.txt', $key . " = " . $value . "\n", FILE_APPEND);
}
NOTE
Unchecked checkboxes will not in the $_POST array.
Related
Firstname *
<input class="input100" type="text" name="name[]" placeholder="Enter Your First Name ">
foreach($_POST['name'] as $value)
{$_SESSION['name'] = array($value);}
I have the option to input multiple names before submitting the form.
i need to save all the name entered in an array so as to later move them to the database.What should i do?
Use the following php code:-
<?php
// $nameInputDataArray will contain the array of names from name[] input fields.
$nameInputDataArray = isset($_POST['name'])? $_POST['name']:array();
foreach ($nameInputDataArray as $name){
// now you can access the names one by one.
echo $name;
}
// or all the names can be returned as , separated string using implode()
echo implode ( "," , $nameInputDataArray );
?>
I'm trying to get the value from two input fields and pass them as the name in a form.
In my code, I am hardcoding the value of the price range in for testing purposes.
echo 'PRICE RANGE:';
echo 'Low: <input type="text" name="t[pr_100000]" value="" maxlength="25" /> High: <input type="text" name="t[ph_10000000]" value="" maxlength="25" />';
echo 'STATUS:';
$termsStatus = get_terms( 'Status', array(
'hide_empty' => 0
) );
echo '<ul>';
foreach ($termsStatus as $term_st) {
$termsStatus = $term_st->name . 'PropertyFilter';
echo '<li><label><input type="checkbox" name="t[st_' . $term_st->name . ']" value="st_">' .$term_st->name. '</label></li>';
}
echo '</ul>';
Here is the code on another page that the search parameters are sent to:
// GETS THE VARIABLE FROM THE SEARCH WIDGET
$array_terms_test = array_keys( $_GET['t'] );
Any suggetions? Thanks in advance!
Judging by your comment, it seems like you are having trouble passing the input fields to the second snippit of code (via a submit button or javascript). You need to surround the input fields with a form tag and submit the form. If you want to use $_GET simply assign action='get' on the form. If for some reason this doesn't work for you, you can use javascript to pull the values from the fields and create a url var which you use to redirect (window.location = url).
Sorry if I didn't understand your question.
This is my checkbox
<input name="interests2" type="checkbox" value="double-deep-racks" />
This is how I am trying to get that value in to a variable
$int = $_POST['interests2'];
Can you please tell me what i am doing wrong. I cant get the values I just get blank.
Try
$int = $_POST['interests2'];
If you are trying to set multiple checkboxes you can do something like,
// Your html
<input type="checkbox" name="interests[]" value="This is i">
<input type="checkbox" name="interests[]" value="Another i value">
// php
$email = "Further Information In: \n";
foreach($_POST['interests'] as $i)
$email .= $i . "\n";
The name of your checkbox is interests2. You must get the value by that name like this:
$int = $_POST['interests2'];
The name element must match what you are looking for. In your input field the name is interests2 but you are looking for interests (missing "2").
Also, you may possibly need to look in $_GET instead of $_POST, depending on the form or the AJAX method you are using (you didn't post that portion of your code).
I have a form with multiple textboxes which are created dynamically, now all these textboxes are of same name lets say txt, now is there any way that when form processing happens we could read all the text boxes values using $_POST method, which are of so called same name. If possible how?
You have to name your textboxes txt[] so PHP creates a numerically indexed array for you:
<?php
// $_POST['txt'][0] will be your first textbox
// $_POST['txt'][1] will be your second textbox
// etc.
var_dump( $_POST['txt'] );
// or
foreach ( $_POST['txt'] as $key => $value )
{
echo 'Textbox #'.htmlentities($key).' has this value: ';
echo htmlentities($value);
}
?>
Otherwise the last textbox' value will overwrite all other values!
You could also create associative arrays:
<input type="text" name="txt[numberOne]" />
<input type="text" name="txt[numberTwo]" />
<!-- etc -->
But then you have to take care of the names yourself instead of letting PHP doing it.
Create your text box with names txt[]
<input type='text' name='txt[]'>
And in PHP read them as
$alTxt= $_POST['txt'];
$N = count($alTxt);
for($i=0; $i < $N; $i++)
{
echo($alTxt[$i]);
}
If you want name, you could name the input with txt[name1], then you could get it value from $_POST['txt']['name1']. $_POST['txt'] will be an associative array.
Let's imagine I got this:
index.php generates form with unpredictable number of inputs with certain IDs/Names and different values that can be edited by user and saved by script.php
<form action="script.php" method="post">
<input id="1" name="1" type="text" value="1"/>
<input id="24" name="24" type="text" value="2233"/>
<input id="55" name="55" type="text" value="231321"/>
</form>
Script.php:
Here I need to get something like array of all inputs that were generated by index.php and save every value that corresponds to its id/name.
Is there a way to do this?
i may be missing something in your question, but the $_POST variable will contain all the name => value pairs you're asking for. for example, in your above HTML snippet:
print_r($_POST);
// contains:
array
(
[1] => 1
[24] => 2233
[55] => 231321
)
// example access:
foreach($_POST as $name => $value) {
print "Name: {$name} Value: {$value} <br />";
}
Use an array_keys on the $_POST variable in script.php to pull out the names you created and use those to get the values.
$keys = array_keys( $_POST );
foreach( $keys as $key ) {
echo "Name=" . $key . " Value=" . $_POST[$key];
}
It sounds like you're using a class or framework to generate your forms, you need to read the documentation for the framework to see if/where it's collecting this data.