A friend told my you can grab stuff from a general $_POST array, but I'm not getting it.
What I have are some values with unique row IDs pulled from a MySQL DB. I have them displayed so they can be updated if necessary en masse. I've given each input form a unique name by concat'ing the MySQL value, but I'm unsure how to grab those values in the script that accepts the $_POST values.
<form action"./" method="POST">
<?php
while($select_row = mysql_fetch_array($select_query))
{
echo $select_row['name'];
echo "</br>";
echo "Week ".$select_row['week'];
echo "</br>";
echo "Hours: ";
?>
<input class="options" type="text" name="hours<?php echo $select_row['submission_id']?>" value="<?php echo $select_row['hours']; ?>" />
<?php
echo "</br></br>";
?>
<input type="submit" value="Submit Hours">
</form>
<?php
}
?>
As you can see, each input will get a name like hours13 or hours144. Without knowing what they will be named beforehand, how can I extract them from $_POST?
Just iterate through the collection:
foreach($_POST as $key=>$value)
{
if($value != '')
{
echo "$key: $value\n";
}
}
Related
I have a form created by a while loop in php like this :
<form action='#' method='post'>
<input type='hidden' name='form_valid' id='form_valid'>
<?php
$i=-1;
$result_array = array();
//the while is from a simple mysql query
while( $line = $results->fetch() )
{
$i++;
echo"<input type='checkbox' class='checkbox' value='".$line->nid."' id='".$i."'>";
echo $line->title;
echo'<br>';
$result_array[$i] = $line->nid;
}
<input type='submit'>
?>
</form>
Then later on the code I'd like to store the values of the checked checkboxes only in a new array :
if (isset($_REQUEST['form_valid'])) //checking is form is submitted
{
foreach($result_array as $result)
{
if($result == $_REQUEST[$j]) <<<< ERROR
{
$final_array[$j] = $result;
}
}
}
Surprisingly, this code does not work at all.
The error message is "Notice : Undefined offset: 0", then offset 1 then 2 etc ...
The line where the message says theres an error is the marked one.
I really have no idea how to do this. Someone ? =)
Don't try to do it this way, this just makes it hard to process, just use a grouping name array: name="checkboxes[<?php echo $i; ?>]", then on the submission, all values that are checked should simply go to $_POST['checkboxes']. Here's the idea:
<!-- form -->
<form action="" method="POST">
<?php while($line = $results->fetch()): ?>
<input type="checkbox" class="checkbox" name="nid[<?php echo $line->nid; ?>]" value="<?php echo $line->nid; ?>" /><?php echo $line->title; ?><br/>
<?php endwhile; ?>
<input type="submit" name="submit" />
PHP that will process the form:
if(isset($_POST['submit'], $_POST['nid'])) {
$ids = $_POST['nid']; // all the selected ids are in here
// the rest of stuff that you want to do with it
}
I've created a textbox so when the admin types a name and clicks submit, it will shows a list of retrieved data from the database.
<form method="post" action="">
<?php
$teacherName = $_POST['teacherName'];
if ($_POST['submitted'] == 1) {
if($teacherName != ""){
$getName = mysql_query("SELECT name, user_id FROM members WHERE name = '$teacherName'");
$teacherdetails = mysql_fetch_array($getName);
$teachername = $teacherdetails['name'];
$teacher_id = $teacherdetails['user_id'];
if($teachername != ""){
print $teachername . "<br/>";
} else {
print "Give a valid name <br/>";
}
}
}
if ($teachername == ""){ ?>
Teacher name:<input type="text" size="20" name="teacherName"><input type="hidden" name="submitted" value="1"><br/>
<input type="submit" value="Submit" />
<?php $getModule = mysql_query("......");
while ($row2 = mysql_fetch_array($getModule)) { ?>
<input type="checkbox" name="modules[]" value="<?php print $row2["module_id"]?>"/> <?php print $row2["module_name"] . '<br/>'; } ?>
</div><br/> <?php } ?>
<input type="submit" value="Submit" />
</form>
Below I wrote this code (in the same script):
<?php
$modules = $_POST['modules'];
for($i = 0; $i < count($modules); $i++){
$module=mysql_query("INSERT INTO module (module_id,user_id) VALUES ('$modules[$i]','$teacher_id')");
}
?>
but for some reason when I call the variable "$teacher_id" (which is the value I retrieved before from the database. It works fine in the form) it returns nothing. It's null but I can't understand why.
Am I doing something wrong?
First off, put the PHP outside the form tags, at the top.
Secondly, the data you are receiving could be an array; with more than one result set;
do this just incase it it returned as that;
foreach($teacherdetails AS $teacher) {
//also set the values of the variables here
echo $teacher['name'];
echo $teacher['user_id'];
}
Regarding the last bit, is the $teacher_id successfully printing a result?
Also, where is the modules being input and posted from?
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 have this code which permits me to retrieve data from a checkbox, there isn't any mysql included yet, so can you help me modify this?
The form is like this:
<form action="checkbox.php" method="post">
<input type="checkbox" name="checkbox[]" value="hostess_name">
<input type="checkbox" name="checkbox[]" value="hostess_familyname_en">
<input type="checkbox" name="checkbox[]" value="hostess_id">
<input type="checkbox" name="checkbox[]" value="hostess_firstname_en">
<br>
<br>
<input type="submit" name="Submit" value="Submit">
</form>
The values i inserted are supposed to be database fields, then i have this checkbox.php file which reads the values selected.
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
echo "Connected to MySQL<br />";
?>
<?
/* and in your checkbox.php you do this: */
if(isset($_POST['Submit']))
{
echo "<pre>"; print_r($_POST);
}
$checked = mysql_real_escape_string(implode(',', $_POST['checkbox']));
echo $checked;
?>
How can assign to checkbox values database fields?
What i'm trying to do is to store the values and export them into a query using implode..
Help me..
Your POST variable ($_POST['checkbox']) is actually already an array. First, to figure out what you are actually working with, do this:
echo '<pre>';
print_r ($_POST['checkbox']);
echo '</pre>';
Then view your script and have a look at the output. Chances are you'll see an array with some keys and values. Using that you can decide how to proceed.
If it were me I would do something like the following to accomplish your task:
$sql = "SELECT `table_id_column`, `another_column` ";
foreach ($_POST['checkbox'] as $key => $value) {
$sql .= ", `$value`";
}
$sql .= " FROM `hostess` ORDER BY `another_colmn` ASC";
Please keep in mind that allowing an SQL statement to be modified in this manner is very bad practice. You'll want to introduce some security into this before putting it on a production environment.
Luke
Use
foreach($_POST[checkbox] as $key => $value {
echo PHP_EOL . "Key is => " . $key . " Value is => " . $value . PHP_EOL;
}
Try this and see the output, you'll see yourself how to proceed further.
I have two array. What I want to do is compare the key ['user_id'] of two arrays, if they are the same, pass the ['user_id'] and ['ref'] in hidden form. I tried to put them into two foreach, but system seems into a dead lock.
<?php foreach($_SESSION['printing_set'] as $data) { ?>
<?php foreach(getProvenaMailableUserlist() as $userlist){ ?>
<input type="hidden" name="reference[<?php echo $data['user_id'] ?>]" value="<? if($userlist['user_id'] == $data['user_id']){echo $userlist['ref'];} ?>" />
<?php } ?>
<?php } ?>
What is the right way to do that?
What you are doing is printing again and again the part of '<input type="hidden" name="...'. here is what you should do
<?php
echo '<input type="hidden" name="reference[' . $data['user_id'] . ']" value="'; //olny one time.
foreach($_SESSION['printing_set'] as $data) {
foreach(getProvenaMailableUserlist() as $userlist){
if($userlist['user_id'] == $data['user_id']){
echo $userlist['ref']; //only if condition is true
}
}
}
echo '" />'; //only one time
?>
You've got some funky formatting going on, so it's hard to tell where the error might be. Try it like this:
<?php
foreach($_SESSION['printing_set'] as $data) {
foreach(getProvenaMailableUserlist() as $userlist){
$value = "";
if($userlist['user_id'] == $data['user_id'])
$value = $userlist['ref'];
echo "<input type='hidden' name='reference$user_id' value='$value' /> \n";
}
}
?>