Trying to link two fields on a POST by getting the ID.
Each 'songfld' has a unique number as below.
The textarea is linked on that number.
The form fields:
<input type="text" name="songfld[901]" value="">
<textarea name="data[901][name]" id="txt-901" rows="2" cols="80"></textarea>
<input type="text" name="songfld[902]" value="">
<textarea name="data[902][name]" id="txt-902" rows="2" cols="80"></textarea>
And trying to get the 901 & 902 ID's here:
foreach($_POST['songfld'] as $val){
$val is the value from input
$id = $_POST['songfld'][0]; // the number in each name="songfld[X]"
$namefld = $_POST['data'][$id]['name']; // gives the value from textarea
echo "<P>$id $idfld $namefld";
}
Of course the value passes, but I'd like to have that unique number from each post as well.
hope someone knows this trick.
Inspect your data like this:
echo "<pre>";
print_r($_POST);
echo "</pre>";
exit;
Also try:
echo "<pre>";
print_r($_POST['songfld']);
echo "</pre>";
exit;
You'll see what is exactly in the arrays.
foreach($_POST['songfld'] as $name => $val){
...
}
Try this:
foreach($_POST['songfld'] as $key => $val){
// $key holds the value you are looking for
}
$sInputFields = '';
for( $i=901;$i<903;$i++ ){
$sInputFields .= '<input type="text" name="songfld[' . $i . ']" value="">';
$sInputFields .= '<textarea name="data[' . $i . '][name]" id="txt-' . $i . '" rows="2" cols="80"></textarea><br>';
}
echo $sInputFields;
You can try this
Related
I have a form like the one below which is posted to contacts.php, and the user can dynamically add more with jQuery.
<input type="text" name="name[]" />
<input type="text" name="email[]" />
<input type="text" name="name[]" />
<input type="text" name="email[]" />
<input type="text" name="name[]" />
<input type="text" name="email[]" />
If I echo them out in PHP with the code below,
$name = $_POST['name'];
$email = $_POST['account'];
foreach($name as $v) {
print $v;
}
foreach($email as $v) {
print $v;
}
I will get something like this:
name1name2name3email1email2email3
How can I get those arrays into something like the code below?
function show_Names($n, $m)
{
return("The name is $n and email is $m, thank you");
}
$a = array("name1", "name2", "name3");
$b = array("email1", "email2", "email3");
$c = array_map("show_Names", $a, $b);
print_r($c);
so my output is like this:
The name is name1 and email is email1, thank you
The name is name2 and email is email2, thank you
The name is name3 and email is email3, thank you
They are already in arrays: $name is an array, as is $email
So all you need to do is add a bit of processing to attack both arrays:
$name = $_POST['name'];
$email = $_POST['account'];
foreach( $name as $key => $n ) {
print "The name is " . $n . " and email is " . $email[$key] . ", thank you\n";
}
To handle more inputs, just extend the pattern:
$name = $_POST['name'];
$email = $_POST['account'];
$location = $_POST['location'];
foreach( $name as $key => $n ) {
print "The name is " . $n . ", email is " . $email[$key] .
", and location is " . $location[$key] . ". Thank you\n";
}
E.g. by naming the fields like
<input type="text" name="item[0][name]" />
<input type="text" name="item[0][email]" />
<input type="text" name="item[1][name]" />
<input type="text" name="item[1][email]" />
<input type="text" name="item[2][name]" />
<input type="text" name="item[2][email]" />
(which is also possible when adding elements via JavaScript)
The corresponding PHP script might look like
function show_Names($e)
{
return "The name is $e[name] and email is $e[email], thank you";
}
$c = array_map("show_Names", $_POST['item']);
print_r($c);
You could do something such as this:
function AddToArray ($post_information) {
//Create the return array
$return = array();
//Iterate through the array passed
foreach ($post_information as $key => $value) {
//Append the key and value to the array, e.g.
//$_POST['keys'] = "values" would be in the array as "keys"=>"values"
$return[$key] = $value;
}
//Return the created array
return $return;
}
The test with:
if (isset($_POST['submit'])) {
var_dump(AddToArray($_POST));
}
This for me produced:
array (size=1)
0 =>
array (size=5)
'stake' => string '0' (length=1)
'odds' => string '' (length=0)
'ew' => string 'false' (length=5)
'ew_deduction' => string '' (length=0)
'submit' => string 'Open' (length=4)
I came across this problem as well. Given 3 inputs: field[], field2[], field3[]
You can access each of these fields dynamically. Since each field will be an array, the related fields will all share the same array key. For example, given input data:
Bob, bob#bob.com, male
Mark, mark#mark.com, male
Bob and his email and sex will share the same key. With this in mind, you can access the data in a for loop like this:
for($x = 0; $x < count($first_name); $x++ )
{
echo $first_name[$x];
echo $email[$x];
echo $sex[$x];
echo "<br/>";
}
This scales as well. All you need to do is add your respective array vars whenever you need new fields to be added.
You can use an array of fieldsets:
<fieldset>
<input type="text" name="item[1]" />
<input type="text" name="item[2]" />
<input type="hidden" name="fset[]"/>
</fieldset>
<fieldset>
<input type="text" name="item[3]" />
<input type="text" name="item[4]" />
<input type="hidden" name="fset[]"/>
</fieldset>
I added a hidden field to count the number of the fieldsets.
The user can add or delete the fields and then save it.
However, VolkerK's solution is the best to avoid miss couple between email and username. So you have to generate HTML code with PHP like this:
<? foreach ($i = 0; $i < $total_data; $i++) : ?>
<input type="text" name="name[<?= $i ?>]" />
<input type="text" name="email[<?= $i ?>]" />
<? endforeach; ?>
Change $total_data to suit your needs. To show it, just like this:
$output = array_map(create_function('$name, $email', 'return "The name is $name and email is $email, thank you.";'), $_POST['name'], $_POST['email']);
echo implode('<br>', $output);
Assuming the data was sent using POST method.
This is an easy one:
foreach($_POST['field'] as $num => $val) {
print ' ' . $num . ' -> ' . $val . ' ';
}
Already is an array.
My inputs are:
<input name="name[]" value='joe'>
<input name="lastname[]" value='doe'>
<input name="name[]" value='jose'>
<input name="lastname[]" value='morrison'>
In the $_POST data, returns the following:
[name] => Array
(
[0] => 'joe'
[1] => 'jose'
)
[lastname] => Array
(
[0] => 'doe'
[1] => 'morrison'
)
You can access to these data, in the following way:
$names = $_POST['name']
$lastnames = $_POST['lastname']
// accessing
echo $names[0]; // joe
This way It is very useful for creating pivot tables.
Using this method should work:
$name = $_POST['name'];
$email = $_POST['account'];
while($explore=each($email)) {
echo $explore['key'];
echo "-";
echo $explore['value'];
echo "<br/>";
}
I have a form like the one below which is posted to contacts.php, and the user can dynamically add more with jQuery.
<input type="text" name="name[]" />
<input type="text" name="email[]" />
<input type="text" name="name[]" />
<input type="text" name="email[]" />
<input type="text" name="name[]" />
<input type="text" name="email[]" />
If I echo them out in PHP with the code below,
$name = $_POST['name'];
$email = $_POST['account'];
foreach($name as $v) {
print $v;
}
foreach($email as $v) {
print $v;
}
I will get something like this:
name1name2name3email1email2email3
How can I get those arrays into something like the code below?
function show_Names($n, $m)
{
return("The name is $n and email is $m, thank you");
}
$a = array("name1", "name2", "name3");
$b = array("email1", "email2", "email3");
$c = array_map("show_Names", $a, $b);
print_r($c);
so my output is like this:
The name is name1 and email is email1, thank you
The name is name2 and email is email2, thank you
The name is name3 and email is email3, thank you
They are already in arrays: $name is an array, as is $email
So all you need to do is add a bit of processing to attack both arrays:
$name = $_POST['name'];
$email = $_POST['account'];
foreach( $name as $key => $n ) {
print "The name is " . $n . " and email is " . $email[$key] . ", thank you\n";
}
To handle more inputs, just extend the pattern:
$name = $_POST['name'];
$email = $_POST['account'];
$location = $_POST['location'];
foreach( $name as $key => $n ) {
print "The name is " . $n . ", email is " . $email[$key] .
", and location is " . $location[$key] . ". Thank you\n";
}
E.g. by naming the fields like
<input type="text" name="item[0][name]" />
<input type="text" name="item[0][email]" />
<input type="text" name="item[1][name]" />
<input type="text" name="item[1][email]" />
<input type="text" name="item[2][name]" />
<input type="text" name="item[2][email]" />
(which is also possible when adding elements via JavaScript)
The corresponding PHP script might look like
function show_Names($e)
{
return "The name is $e[name] and email is $e[email], thank you";
}
$c = array_map("show_Names", $_POST['item']);
print_r($c);
You could do something such as this:
function AddToArray ($post_information) {
//Create the return array
$return = array();
//Iterate through the array passed
foreach ($post_information as $key => $value) {
//Append the key and value to the array, e.g.
//$_POST['keys'] = "values" would be in the array as "keys"=>"values"
$return[$key] = $value;
}
//Return the created array
return $return;
}
The test with:
if (isset($_POST['submit'])) {
var_dump(AddToArray($_POST));
}
This for me produced:
array (size=1)
0 =>
array (size=5)
'stake' => string '0' (length=1)
'odds' => string '' (length=0)
'ew' => string 'false' (length=5)
'ew_deduction' => string '' (length=0)
'submit' => string 'Open' (length=4)
I came across this problem as well. Given 3 inputs: field[], field2[], field3[]
You can access each of these fields dynamically. Since each field will be an array, the related fields will all share the same array key. For example, given input data:
Bob, bob#bob.com, male
Mark, mark#mark.com, male
Bob and his email and sex will share the same key. With this in mind, you can access the data in a for loop like this:
for($x = 0; $x < count($first_name); $x++ )
{
echo $first_name[$x];
echo $email[$x];
echo $sex[$x];
echo "<br/>";
}
This scales as well. All you need to do is add your respective array vars whenever you need new fields to be added.
You can use an array of fieldsets:
<fieldset>
<input type="text" name="item[1]" />
<input type="text" name="item[2]" />
<input type="hidden" name="fset[]"/>
</fieldset>
<fieldset>
<input type="text" name="item[3]" />
<input type="text" name="item[4]" />
<input type="hidden" name="fset[]"/>
</fieldset>
I added a hidden field to count the number of the fieldsets.
The user can add or delete the fields and then save it.
However, VolkerK's solution is the best to avoid miss couple between email and username. So you have to generate HTML code with PHP like this:
<? foreach ($i = 0; $i < $total_data; $i++) : ?>
<input type="text" name="name[<?= $i ?>]" />
<input type="text" name="email[<?= $i ?>]" />
<? endforeach; ?>
Change $total_data to suit your needs. To show it, just like this:
$output = array_map(create_function('$name, $email', 'return "The name is $name and email is $email, thank you.";'), $_POST['name'], $_POST['email']);
echo implode('<br>', $output);
Assuming the data was sent using POST method.
This is an easy one:
foreach($_POST['field'] as $num => $val) {
print ' ' . $num . ' -> ' . $val . ' ';
}
Already is an array.
My inputs are:
<input name="name[]" value='joe'>
<input name="lastname[]" value='doe'>
<input name="name[]" value='jose'>
<input name="lastname[]" value='morrison'>
In the $_POST data, returns the following:
[name] => Array
(
[0] => 'joe'
[1] => 'jose'
)
[lastname] => Array
(
[0] => 'doe'
[1] => 'morrison'
)
You can access to these data, in the following way:
$names = $_POST['name']
$lastnames = $_POST['lastname']
// accessing
echo $names[0]; // joe
This way It is very useful for creating pivot tables.
Using this method should work:
$name = $_POST['name'];
$email = $_POST['account'];
while($explore=each($email)) {
echo $explore['key'];
echo "-";
echo $explore['value'];
echo "<br/>";
}
I am trying to achieve dynamic $row values from an array, and show them as output. It keeps showing nothing while the values are already there.
This is what I have so far, where:
$row["lg_".$val.""]; should return:
$lg_it
'it' is the $val from the array.
foreach($arrMapCookieToLang as $key => $val) {
$shrtKey = $row["lg_".$val.""];
<input type="text" name="lg_$val" value="$shrtKey">
}
Anyone an idea?
What you have should result in a syntax error. Try the following:
<?php
foreach($arrMapCookieToLang as $key => $val) {
$shrtKey = $row['lg_'.$val];
?>
<input type="text" name="lg_<?= $val ?>" value="<?= $shrtKey ?>">
<?php
}
You've missed to echo your input-field:
foreach($arrMapCookieToLang as $key => $val) {
$shrtKey = $row["lg_".$val.""];
echo '<input type="text" name="lg_' . $val .'" value="' . $shrtKey . '">';
}
Further, if you don't use the array key in the foreach-loop, you can omit the $key =>-part and just write
foreach($arrMapCookieToLang as $val) {
// ...
}
Instead, the
<input type="text" name="lg_$val" value="$shrtKey">
Maybe you should use the
echo "<input type=\"text\" name=\"lg_" . "$val\" value=\"$shrtKey\">";
So basically, I have an array of Modules and I want to have a drop-down menue that users can then select what grade they got. This works fine, however, I would like the results to be stored inside of an array, to however many values they selected. So for example:
If someone selected "40" in Mod1 and in Mod2 they selected "20" then the array would be like this:
mod1=>40
mod2=>20
...
Here is the code so far, it's probably something stupid, I just cannot get my head around it.
<?php
$modules = array('Mod1', 'Mod2', 'Mod3');
if(!isset($_POST['submitted']))
{
echo '<form method="post">';
echo 'Please enter the grades you got for each Module: <br />';
foreach($modules as $module)
{
echo $module . ': <input type="text" name="grades[]" value=""> <br />';
}
echo '<br /><input type="submit" name="submit" value="Go!">';
echo '<input type="hidden" name="submitted" value="TRUE">';
}else{
$input = $_POST['score[]'];
foreach($modules as $i => $module){
$input[$module] = $input[$i];
var_dump($input[$module] = $i);
//unset($input[$i]);
}
//var_dump($input);
}
?>
<select name="score[<?php echo $module; ?>]">
should get you going :)
the array will look exactly like you narrowed it down in the intro.
You could use a name that groups the values of the selects in POST into one array:
echo '<select name="score[]">';
You can then use:
$input = $_POST['score[]'];
foreach($modules as $i=>$module){
$input[$module] = $input[$i];
unset($input[$i]);
}
var_dump($input);
just change your name attribute to an array:
echo '<select name="score[]">';
the the $_POST variable will be in an array
I am developing an ordering page in PHP for some products. The user needs to be able to select multiple types of products, so checkboxes will be necessary in the HTML form.
I've established an array for the checkboxes via the "name" attribute.
My problem is, I want to display what the user has selected on a confirmation page, so I'd like the "value" of those checkboxes to ultimately return a product name AND a product price. As far as I can tell I'm going to be stuck with one value only, so I've attempted to get around this:
<?php
//variables for the array and the count
$parray = $_POST['product'];
$pcount = count($parray);
//arrays for each product's information; i've only listed one for simplicity
$br = array("Big Red", 575);
/*The idea for this was for each product ordered to add a line giving the product information and display it. When the $key variable is assigned, it is stored simply as a string, and not an array (yielding [0] as the first letter of the string and [1] as the second), which I expected, but my general idea is to somehow use $key to reference the above product information array, so that it can be displayed here*/
if (!empty($parray))
{
for ($i=0; $i < $pcount; $i++)
{
$key = $parray[i];
echo "<tr><td height='40'></td><td>" . $key[0] . "</td><td>" . $key[1] . "</td></tr>";
}
}
?>
Is there anyway to make my $key variable actually act as if it is the array's name it is set to?
If not, is there any good way to do this?
So in your HTML table, you'll need to render each checkbox like this:
<input type="checkbox" name="selectedIDs[]" value="$key" />
where $key is the item number.
Then, in your PHP, you'll have a $_POST["selectedIDs"] variable, which is an array of all the item numbers that were checked.
Assuming you've got a product list array like this:
$products = array( 1 => array("Big Red", 575), 2 => array("Spearmint", 525));
You can then print a list of the selected products using a loop like this:
for($i = 0; $i < count($_POST["selectedIDs"]); $i++) {
$key = $_POST["selectedIDs"][$i];
$product = $products[$key];
echo "<tr><td height='40'></td><td>" . $product[0] . "</td><td>" . $product[1] . "</td></tr>";
}
The only real difference between this and what you wrote is that my $products array is two-dimensional, and my $key is used to grab the relevant product from the $products array.
You can give the value of the options the value of the ID of the corresponding item. Then you'll receive an array of ID's, which you can then again map to the $br array, where you look up the name and price of an item by its ID. You could use the array key as ID, so:
<!-- Form: -->
<input type="checkbox" name="product[]" value="1" />
<input type="checkbox" name="product[]" value="..." />
<input type="checkbox" name="product[]" value="15" />
[...]
<?php
$product = array();
$product[1] = array('name' => "Big Red", 'price' => 575);
$product[...] = array('name' => "...", 'price' => ...);
$product[15] = array('name' => "Small Green", 'price' => 475);
foreach ($_POST['product'] as $pId)
{
echo $products[$pId]['name'] . " costs " . $products[$pId]['price'];
}
?>
If of course your array originates from a database, you can use the table's ID's as values for the checkboxes. You could then implode the $_POST['product'] (of course after escaping it) with a comma, and use it in a SELECT ... WHERE ID IN ($productIds) query.
If i'm understanding you correctly, you'll have checkboxes something like this:
<input type="checkbox" name="product_id[]" value="1">Product #1<br/>
<input type="checkbox" name="product_id[]" value="2">Product #2<br/>
<input type="checkbox" name="product_id[]" value="3">Product #3<br/>
That should post whatever is checked as an array to your server.
gen_paper1.php
<div style="overflow: auto; height: 500px; border: 1px solid;">
<form action="" method="post">
<fieldset style="font-size: 15px; font-weight: bolder;">
<h4><u>Your Existing Header's are listed below</u> </h4>
<hr / >
<?php
$xhdr = mysql_query("SELECT * from s_user_header ORDER BY 'ASC'");
while($rheader = mysql_fetch_array($xhdr)){
$h = $rheader['h_content'];
$hid = $rheader['h_id'];?>
<script>
$(document).ready(function(){
//$('input[type=checkbox]').val('<?php echo $hid ; ?>').tzCheckbox({labels:['<?php echo $h; ?>','<?php echo $h; ?>']});
//$('#c').tzCheckbox({labels:['<?php echo $h; ?>','<?php echo $h; ?>']});
$('').tzCheckbox({labels:['<?php echo $h; ?>','<?php echo $h; ?>']});
});
</script>
<table>
<td><input type="checkbox" id="c" name="u_hdr[]" value="<?php echo $hid; ?>"></td>
<td style="font-weight: bolder"><?php echo $h."<br />"; ?></td>
</table>
<?php } ?>
</fieldset>
</div>
gen_paper2.....3.....
i need all this values in further page, so have taken hidden variables
<input type="hidden" name="hstdid" value="<?php echo $stdid; ?>">
<input type="hidden" name="hsubid" value="<?php echo $subid; ?>">
<input type="hidden" name="hdifid" value="<?php echo $difid; ?>">
<input type="hidden" name="hmarks" value="<?php echo $t_marks; ?>">
<input type="hidden" name="h_hid[]" value="<?php echo $hh; ?>">
gen_paper4.php
$getstdid = $_POST['hstdid3'] ;
$getsubid = $_POST['hsubid3'] ;
$getdifid = $_POST['hdifid3'] ;
$gethmarks = $_POST['htmarks'] ;
$hdr= $_POST['h_hdrid'] ;
$h = implode($hdr);
<table>
<?php
$h1 = explode(",",$h);
count($h1) . "<br />";
for($i=0;$i<count($h1);$i++){
$h1[$i];
$xheader = mysql_query("SELECT * from s_user_header WHERE h_id = ".$h1[$i]);
while($row = mysql_fetch_array($xheader)){ ?>
<tr>
<td><b>
<?php echo $i + 1 . ".   "; ?>
</b></td><td>
<?php echo $header = $row['h_content'] . "<br />";
?></td>
</tr>
<?php
}
}
//echo $header; ?>
</table>