Posting input form array in another php page - php

I have this piece of code which permits me to print multiple dates, so this is the form, which gets populated with dates separated by a comma, like 19/02/1990,12/12/1220 etc etc..
Now, what i need to do is to save them in an array and then print the array!
For the moment i have this code, could you please help me?
Thanks!
<form action="insert_date.php" method="post">
<div id="with-altField"></div>
<input type="text" id="altField" name="altField">
</div>
<input type="submit" value="Submit" />
</form>
This doesn't work if i select more than one date, but if i select one, it does, that's why i need to save it in an array form!
insert_date.php
<?php
$date=$_POST['altField'];
echo $date;
?>

$dates = explode(",", $_POST['altField']);
foreach($dates as $date){
echo htmlentities($date, ENT_QUOTES, 'utf-8') . '<br />';
}

Try This
<?php
$dates = $_POST['altField'];
$arrOfDates = explode(',', $dates);
var_dump($arrOfDates);
foreach($arrOfDates as $date)
{
echo $date;
}
?>

On insert_date.php you can explode by ,
<?php
$date=$_POST['altField'];
$explode = explode(',', $date);
?>
then you can use foreach to parse your dates
<?php
foreach($explode as $date){
echo trim($date);
}
?>

Related

php - How to insert looped value of checkbox into a single mysql_query

I have here a looped value of checkboxes let's assume I have 3 increments
<?php foreach ($times as $time) { ?>
<input type="checkbox" name="b" id="b1" value=" <?php echo $time->format('h:i a'), '-', $time->add($interval)->format('h:i a'); ?>">
<?php } ?>
In the foreach it display 3 checkboxes that has different value.
Now for example i checked 2 boxes. The query must run twice? Or better yet, the 2 checked value must be inserted to the db.
$b = $_POST['b'];
$ins = mysql_query("INSERT INTO ehr_schedule3(`sched3_time) VALUES('$b')");
How can I do that?
All your checkboxes have the same name, so $_POST['b'] will just get the value of the last one. To get multiple values, you need to use an array-style name:
<?php foreach ($times as $time) { ?>
<input type="checkbox" name="b[]" value="<?php echo $time->format('h:i a'), '-', $time->add($interval)->format('h:i a'); ?>">
<?php } ?>
Then $_POST['b'] will be an array of all the values.
foreach ($_POST['b'] as $b) {
$b = mysql_real_escape_string($b);
$ins = mysql_query("INSERT INTO ehr_schedule3(`sched3_time) VALUES('$b')");
}
I also took out id="b1" because you shouldn't have multiple elements with the same ID. If you really need the inputs to have IDs (why?), you'll need to give them different ones, perhaps by using the array index as part of the ID.
You can put a counter to count bs:
<?php
$count = 0;//to count the rows if they are dynamic
foreach ($times as $time) {
?>
<input type="checkbox" name="b<?php echo $count; ?>" value="<?php echo $time->format('h:i a'), '-', $time->add($interval)->format('h:i a'); ?>">
<?php
$count++;
}
?>
<input name="rowCount" type="hidden" value="<?php echo $count; ?>" /><!-- to store number of inputs -->
then in your insert function
$b_count = $_POST['rowCount'];//to store count of b
$my_quries = [];
for($i=0; $i<$b_count; $++){
$b = $_POST['b'.$i];//get the corresponded b from POST req
$my_quries[] = mysql_query("INSERT INTO ehr_schedule3(`sched3_time) VALUES('$b')");
//you can execute the query in this loop or another one as you like
}

Array of select values

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

PHP merging two arrays

I am trying to merge two arrays with array_merge(), but I am receiving the following warning:
Warning: array_merge() [function.array-merge]: Argument #1 is not an array on line 41
Here is the code:
$travel = array("Automobile", "Jet", "Ferry", "Subway");
echo "<ul>";
foreach ($travel as $t)
{
echo "<li>$t</li>";
}
echo "</ul>";
?>
<h4>Add more options (comma separated)</h4>
<form method="post" action="index2.php">
<input type="text" name="added" />
<?php
foreach ($travel as $t){
echo "<input type=\"text\" name=\"travel[]\" value=\"$t\" />\n";
}
?>
<input type="submit" name="submit" value="Add" />
</form>
<?php
$travel = $_POST["travel"];
$added = explode(",", $_POST["added"]);
$travel = array_merge($travel, $added);
print_r ($travel);
?>
You're assigning $_POST["travel"], which is not an array but a string, to $travel. Turn it into an array first.
You are accessing $_POST["travel"] but it's not defined if you didn't submit the form. You need to check if it's a post request:
<?php
if(isset($_POST["travel"])){
$travel = $_POST["travel"];
$added = explode(",", $_POST["added"]);
$travel = array_merge($travel, $added);
}
print_r ($travel);
?>
$_POST is an array, $_POST['travel'] however is just an element unless its originating from a multiselect element.

Add user input to array

I am trying to add user input to a previously created array with array_merge. However, I am having trouble echoing the entire, new array as an unordered list. the user's entry is being processed correctly, but the original array is displaying as "Array" within the unordered list. Here is the code:
<?php
$travel = array("Automobile", "Jet", "Ferry", "Subway");
foreach ($travel as $t)
{
echo "<ul>";
echo "<li>$t</li>";
echo "</ul>";
}
?>
<form action="arrays.php" method="post">
<input type="text" name="added" />
<?php
foreach ($travel as $t)
{
echo "<input type=\"text\" name=\"travel[]\" value=\"$t\" />";
}
?>
<input type="submit" name="submit" value="Add More!" />
</form>
<?php
$travel = array($_POST["travel"]);
$added = explode(",", $_POST["added"]);
$travel = array_merge($travel, $added);
echo "<p> Here is the list with your additions:</p>";
echo "<ul>";
foreach ($travel as $t)
{
echo "<li>$t</li>";
}
echo "</ul>";
?>
Try using a new variable name for the new array created by array_merge(). I think you may run into problems modifying the array you're storing into.
$travel = array($_POST["travel"]);
should be
$travel = $_POST['travel'];
The problem was solved thus:
if (isset($_POST["submit"]))
{
$travel = $_POST["travel"];
$added = explode(",", $_POST["added"]);
$travel = array_merge($travel, $added);
echo "<p> Here is the list with your additions:</p>";
echo "<ul>";
foreach ($travel as $t)
{
echo "<li>$t</li>";
}
echo "</ul>";
}
?>

Why does my php drop down form result in an Undefined offset

Please could you check this code and see why its returning an Undefined offset: and how it can be fixed.
options.php
<?php
$options = array();
$options["PC 1"] = array("year"=>"2000","colour"=>"blue");
$options["PC 2"] = array("year"=>"2003","colour"=>"pink");
$options["PC 3"] = array("year"=>"2006","colour"=>"orange");
?>
index.php
<html>
<body>
<form name="input" action="test.php" method="post">
Device Name: <?php
include("options.php");
echo '<select name="option">';
foreach($options as $name => $values)
{ echo '<option value="' .$name .'">' .$name .'</option>';
}
echo '</select>';
?>
<input type="submit" value="Submit" />
</form>
</body>
</html>
test.php
<?php
include("options.php");
$chosenValue = $_POST['option'];
list($year,$colour) = $options[$chosenValue]; ---- here is the error line
echo $year;
echo $colour;
?>
Thanks
I think it's because the key's in your array $options have spaces in them. Which is completely valid/legal in PHP, but in HTML, when the form is submitted it doesn't like it.
Try changing them to $options["PC1"] and so on and see if it fixes it.
Edit: From the PHP manual - list() only works on numerical arrays and assumes the numerical indices start at 0.
Try changing test.php to:
<?php
include("options.php");
$chosenValue = $_POST['option'];
$year = $options[$chosenValue]['year'];
$colour = $options[$chosenValue]['colour'];
echo $year;
echo $colour;
?>
Change the way you access year and colour in test.php, list() doesn't seem to work well with non-numeric indices. One of the user-contributed comments in the php docs suits your needs
http://www.php.net/manual/en/function.list.php#53420
Or just go for
$data = &$options[$chosenValue];
echo $data['year']; // etc

Categories