I have a div in my page called .highlights.
In this div I have a unknown numbers of text input(<input type="text" />). It can range from 0 to unknown.
When someone clicks at submit, I want to store in PHP all the values of the inputs, into one variable called myHighlights. The values must be seperated by ;
<input type="text" name="unlimited[]" />
if( isset($_POST['submit_button']) ) {
// Skip blank values
$unlimited = array_filter( $_POST['unlimited'] );
$myHighlights = implode(';', $unlimited);
}
To begin with, you'll have to assign names to the controls so they get sent together with the rest of of the form. Please have a look at the How do I create arrays in a HTML <form>? entry of the PHP FAQ for a nifty trick.
if($_POST)
{
$myHighlights = implode(';',$_POST);
print_r($myHighlights);
}
Related
I want the Code below to read individual line of text from dataFile.txt and show it in input field.
Problem is After reading first line from text document it shows all remaining lines of text from text file into input field. But on clicking submit it should show second line only then again on submitting it should show third line only, inside input field. please help.
<?php
$file = __DIR__."/dataFile.txt";
$f = fopen($file, "r");
$array1 = array();
<form action="datagGet.php" method="get">
<input type="text" value="
<?php while ( $line = fgets($f, 100) )
{
$nl = mb_strtolower($line);
echo $nl;
if(isset($_GET['done']))
{
$nl++;
}
else
{
break;
}
}
?>"
name="someText">
<input type="submit" name="done" >
</form>
You have several problems with you code. And the first comment above points to many of the. Key is the fact that the $_GET['done'] is set for the form submit and therefore you will echo all the lines of the output. It never breaks.
Also there is the fact that you are opening the file for reading each submit of the form. Although I don't see a simple way around this unless you store the file contents between requests.
One possible option is to use 'file()' to read the entire contents into an array. And then use sessions to store which line has been read. Then on each submit, look for the index of the array from the session read; advance it by one read the file again and return that line. Wow wasteful. But okay for simple site.
so use file to get the lines in an array.
output the first line into the value.
store the next index to be read in the $_SESSION variable like $_SESSION['next_line'] = 1
then upon further submissions. read it all back in. look up the 'next_line', and output that line.
so, for example
$array = file('your file name');
$output = $array[0];
if (isset($_SESSION['next_line']))
$_SESSION['next_line'] = intval($_SESSION['next_line']) + 1;
else
$_SESSION['next_line'] = 1;//prime the pump
echo the form with $output
then rinse and repeat. e.g. read, get output (next_line) with file, set $_session = next_line + 1; render output in form.
ps. some extra notes
* of course you'll need to start session on each request.
* you'll need to check if the $_SESSION['next_line'] is set. if not, set it to 1 (prime it)
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.
I have a PHP form which shows one 'User details' block by default. 'User details' section has first name, last name and address etc. I have a requirement to repeat the same block after clicking on the button so the user can enter 'n' number of users into on the form. Also I need to save all the information in MySQL database. What is the best way to achieve this? Please let me know. I appreciate any help.
Thank you.
I would use jQuery to add additional form elements. You'll need to give each element a different 'name' attribute though- I'd use something like name='user1name' (and in the next username field name='user2name'). Comment on my post if you need specific help with that. When the form submits, I'd use php to loop through each set of fields with an insert statement at the end of the loop.
PHP is something like this:
for ($i = 1; is_null($_POST['name' . $i]) == false; $i++) {
$name = $_POST['user' . $i . 'name'];
// repeat for all your fields
// do your insert statement here - 1 insert per user created
mysql_query("INSERT INTO table_name (Name, Address, ...) VALUES ('$name', '$address', ...)");
}
jQuery is something like this:
Existing HTML
Should only have 1 div with class=user at this point
<div id='fields'>
<div class='user'>
<input type='text' name='user1name' />
<input type='text' name='user2name' />
... more elements ...
</div>
</div>
jQuery
var userFields = $("div.user").clone();
$(userFields).appendTo("div#fields");
$("div#fields").children("div.user:last").children().each(function() {
// change names here!
});
This will copy the existing field and add its copy to the fields div. It will also set the new names for the second set of user fields. Note: you only need ot set userFields once, and then you can use $(userFields).appendTo("div#fields"); as many times as you need to (as long as the userFields variable is in scope).
If you have event handlers attached to your elements in the user field, use $("div.user").clone(true, true); instead of $("div.user").clone();.
I've just started using jQuery. One thing I've been using it for is adding rows to a table that is part of a form.
When I add a new row, I give all the form elements names like 'name_' + rowNumber. I increment rowNumber each time I add a row.
I also usually have a Remove Row Button. Even when a row is removed, the rowNumber count stays the same to keep from repeating element names.
When the form is submitted, I set a hidden element to equal the rowNumber value from jQuery. Then in PHP, I count from 1 to the rowNumber value. Then for each value, I perform an isset($_REQUEST['name'_ . index]). This is how I extract the form elements that remained after deleting rows in jQuery.
Does anyone here have a better technique for accounting for deleted rows?
For some of our simpler tables, we use a field name such as 'name[]', though for JavaScript they would need a usable id.
It does add some complexity in that 'name[0]' has to assume 'detail[0]' is the correct element.
PHP will create an array and append elements if the field name ends with [] similar to
<input name="field[]" value="first value" />
<input name="field[]" value="second value" />
// is roughly the same as
$_POST['field'][] = 'first value';
$_POST['field'][] = 'second value';
Use arrays to hold you values in your submission. So bin the row count at the client side, and name your new elements like name[]. This means that $_POST['name'] will be an array.
That way at the server side you can easily get the row count (if you need it) with:
$rowcount = count($_POST['name']);
...and you can loop through the rows at the server side like this:
for ($i = 0; isset($_POST['name'][$i]; $i++) {}
You could extract all the rows by doing a foreach($_POST as $key => $value).
When adding a dynamic form element use the array naming method. for example
<input type="text" name="textfield[]" />
When the form is posted the textfield[] will be a PHP array, you can use it easily then.
When you remove an element make sure its removed from the HTML DOM.
Like blejzz suggests, I think if you use $_GET, then you can just cycle through all of the inputs that were sent, ignoring the deleted rows.
foreach ($_GET as $k=>$v) {
echo "KEY: ".$k."; VALUE: ".$v."<BR>";
}
I notice that you mention "accounting for deleted rows"; you could include a hidden input, and add a unique value to it each time someone deletes a row. For example, the input could hold comma-separated values of the row numbers:
<input type="hidden" value="3,5,8" id="deletions" />
and include in your jQuery script:
$('.delete').click(function(){
var num = //whatever your method for getting the row number
var v = $('#deletions').val();
v = v.split(',');
v.push(num);
v = v.join(',');
$('#deletions').val(v);
});
Then you should be able to know which rows were deleted (if that is what you were looking for).
you can use POST or GET
After submit you can use all of your form element with this automaticly. You dont need to reorganise your form element names. Even you dont need to know form elements names.
<form method="POST" id="fr" name="fr">.....</form>
<?php
if(isset($_POST['fr'])){
foreach($_POST as $data){
echo $data;
}
}
?>
Also you should look this
grafanimasyon.blogspot.com.tr/2015/02/veritabanndan-php-form-olusturucu.html
This is a automated form creator calcutating your database tables. You can see how to give name to form elements and use them.
How would I go about parsing incoming form data where the name changes based on section of site like:
<input type="radio" name="Motorola section" value="Ask a question">
where 'Motorola section may be that, or Verizon section, or Blackberry section, etc.
I do not have any control over changing the existing forms unfortunately, so must find a way to work with what is there.
Basically, I need to be able to grab both the name="" data as well as its coresponding value="" data to be able to populate the email that gets sent properly.
Well, you don't receive a HTML form, but just field names and values in $_POST. So you have to look what to make out of that.
Get the known and fixed fields from $_POST and unset() those you've got [to simplify]. Then iterate over the rest. If " section" is the only constant, then watch out for that:
foreach ($_POST as $key=>$value) {
if (stristr($key, "section")) {
$section = $value;
$section_name = $key;
}
}
If there are multiple sections (you didn't say), then build an section=>value array instead.
<form action="formpage.php" method="post">
<input type="radio" name="Motorola_section" value="Ask a question">
</form>
$motorola = $_POST['Motorola_section'];
if ($motorola =='Ask a question')
{
form submit code if motorola is selected
}
Well, first off, you shouldn't have spaces in the name field (even though it should work with them).
Assuming it's a form, you can get the value through the $_POST (for the POST method) and $_GET (for the GET method) variables.
<?php
if ( isset( $_POST['Motorola section'] ) ) // checks if it's set
{
$motoSec = $_POST['Motorola section']; // grab the variable
echo $motoSec; // display it
}
?>
You can also check the variables using print_r( $_GET ) or print_r( $_POST ).