I have made a form with checkboxes and 2 dropdowns populated by mysql.
I can get both to work on separate pages but cannot merge together in one form.
Here is my form code
<?php
mysql_connect("localhost","root","") or die (mysql_error());
mysql_select_db("test") or die (mysql_error());
$query = "SELECT name from aa";
$result = mysql_query($query);
?>
<form method = "post" action = "check2code.php">
<select name = "select1">
<?php
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>
<option value="<?php echo $line['name']; ?>"> <?php echo $line ['name']; ?></option>
<?php
}
?>
</select>
<input type="submit" value="Submit" />
</form>
Here is the php im sending to
<?php
print_r($_POST['day']);
print_r ($_POST['select1']);
?>
the error i get is
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 8 [5] => 9 [6] => 10 [7] => 16 )
Notice: Undefined index: select1 in /opt/lampp/htdocs/test/check2code.php on line 5
so the check box is fine the dropdown is not.
Maybe that no option is selected? The var $_POST['select1'] is only initialized if there is at least one option selected. Just try to select an option before you send the forms data to the server.
Try with mysql_fetch_assoc, not mysql_fetch_array.
Related
i'm stuck in a weird problem with a page
<table class='table'>
<?
print_r($selected_working_time);
// it print a previous selection of working time. for example:
// Array ( [0] => 5 [1] => 6 [2] => 4 [3] => 7 [4] => 9 [5] => 8 )
$query="SELECT Working_time_code, Working_time_description
FROM Working_time
ORDER BY Working_time_description";
// a table with a working_time code and his description. for example:
// 1 - Part-time; 2 - Full-time, ..., 9 - 10:00 - 16:00
if($stmt = $connection->prepare($query)) {
$stmt->execute();
while($all_working_time = $stmt->fetch(PDO::FETCH_ASSOC)) {
$desc = trim($all_working_time['Working_time_description']);
$code = $all_working_time['Working_time_code'];
echo"<tr><td>$desc</td>";
if(in_array($code, $selected_working_time)) { // if code is in my selection, YES checkbox is checked, else NO checkbox is checked.
echo "<td><label class='radio-inline'><input type='radio' name='$codice' value='Y' checked />YES</label>
<label class='radio-inline'><input type='radio' name='$codice' value='N' />NO</label></td>";
}
else {
echo "<td><label class='radio-inline'><input type='radio' name='$codice' value='Y' />YES</label>
<label class='radio-inline'><input type='radio' name='$codice' value='N' checked />NO</label></td>";
}
echo"</tr>";
}
}
?>
</table>
This code works fine, because when i show source code of my generated page i see correctly checked checkbox, but no one of them is really checked.
Where is the problem? It's like a visualization problem, it isn't working with firefox and chrome.
Thank you.
I have a mysql table with two columns; id and type. I'm trying to retrieve those values to use in a select list of values (aka drop down list). Outside of my html, this php works perfectly:
$sql = "SELECT * FROM `usertype`";
$query = mysqli_query($con, $sql);
while ($type_lov = mysqli_fetch_assoc($query)) {
echo '<pre>', print_r($type_lov,true), '</pre>';
};
Output from php above:
Array ( [id] => 1 [type] => System Admin )
Array ( [id] => 2 [type] => System Admin2 )
Array ( [id] => 3 [type] => System Admin3 )
Array ( [id] => 4 [type] => Account Admin )
Array ( [id] => 5 [type] => Account User )
To get it into the SELECT / OPTIONS tags, I attempted several things unsuccessfully. The two attempts that made the most sense to me (but that did not work) were:
<!--ATTEMPT 1-->
<select>
<?php while ($type_lov = mysqli_fetch_assoc($query)) {
foreach ($type_lov as $id=>$type) { ?>
<option value="<?php echo $id; ?>"><?php echo $type; ?></option>
<?php };
}; ?>
</select>
<!--ATTEMPT 2-->
<select>
<?php foreach ($type_lov = mysqli_fetch_assoc($query) as $id=>$type) { ?>
<option value="<?php echo $id; ?>"><?php echo $type; ?></option>
<?php }; ?>
</select>
Neither worked. What is the proper way to go about this?
You should read up on mysqli_fetch_assoc. It returns it's data as an associative array, meaning your table columns are used as indices for the array.
Furthermore, why are you coupling while with foreach in your first example? What's the thought process that you went on?
Anyway, this should help you on your journey:
<select>
<?php
while (($data = mysqli_fetch_assoc($query)))
{
echo '<option value="' . $data['id'] . '">' . $data['type'] . '</option>';
}
?>
</select>
Here is my code.
I made a form with multiple rows which will be updated in a single submit.
I searched in forums, but didn't find the exact answer.
<form method='POST' action='demo24.php'>
<table width="500px" height="500px">
<tr><th>SETNAME</th><th>POST</th></tr>
<?php
$query = "SELECT name,setid FROM `set` LIMIT 0 ,10";
$result = mysql_query($query) or die(mysql_error()."[".$query."]");
while($row = mysql_fetch_array($result))
{
$output .='<tr><td><input type="text" value="'.$row['name'].' '.$row['setid'].'" name="name'.$row['setid'].'">'.$row['name'].'</td><td><input type="checkbox" value="'.$row['setid'].'" name="setid"></td></tr>';
}
echo $output;
?>
</td><td><input type='submit' value='submit' name='submit'/></td></tr>
</table>
</form>
Note that you can use something like name[] in multiple input fields to send an array to the server:
<input type="text" name="row[]" />
<input type="text" name="row[]" />
On the server side $_POST['row'] will look like this:
Array (
[0] => first_input,
[1] => second_input
)
You can do this for all fields (id, row, etc.) and then loop through $_POST['id'] etc. to get all entries back together. Be sure to validate enough, i.e. make sure that those fields are indeed arrays, that all are of the same size, etc.
The MySQL family of PHP is deprecated and support thereof will disappear. Please look into PDO or MySQLi to execute SQL code instead.
If your form method as 'post' then you can use this code :
if(isset($_POST['submit']))
{
$query = "SELECT name,setid FROM `set` LIMIT 0 ,10";
$result = mysql_query($query) or die(mysql_error()."[".$query."]");
while($row = mysql_fetch_array($result))
{
$id = $row['setid'];
$name = $_POST['name'.$id];
//here will be you update code. set value will be $name and update where setid = $id
}
}
i hope someone can help me. i have 4 checkboxes i want to save it in one string in my database,for example you only choose 2 or 3 checkboxes options.. the selected values only will save in one field of my database..
<tr>
<td><label>Plan Description:</label></td>
</table>
<?php
$des_query=mysql_query("SELECT * FROM addons;") or die ("Note: ". mysql_error());
?>
<?php
while($desc = mysql_fetch_array($des_query))
{
echo "<input type='checkbox' name='addons[]' value=".$desc['id'].">". $desc['name']."</input><br/>";
}
?>
</tr>
$insStr = '';
foreach($_POST['addons'] as $val){ $insStr .=$val.","; }
mysql_query("INSERT INTO promos (promo) VALUES ( '$insStr' )");
You can use serialize() method
example
$arr = Array ( [0] => 1 [1] => 2 [2] => 3);
$str = serialize($arr);
After retrieving from database use unserialize() method
You can take advantage of PHP built-in function named implode
Example
$checkboxValue implode(',',$_POST['addons']);
Try like this
if(!empty($_POST['addons'])) { $addons_string="";foreach($_POST['addons'] as $addon){$addons_string.=$addon.","; } }
test.php
<form name="test" action="test1.php" method="get">
<?php
for($i=0; $i<9;$i++){
echo "<input type='checkbox' name=a[] value=$i>$i<br>";
}
?>
<input type="submit" value="Submit">
</form>
and test1.php
<?php
foreach($_GET['a'] as $check) {
echo $check;
//SAVE VALUES TO DATABASE
}
?>
Hope it will help you
Here's my database:
id name parent_id
1 Computers NULL
2 Apple 1
3 Books 1
4 Music NULL
5 CDs 4
6 Records 4
My categories function:
public function showCategories($parent_id = 0){
if($parent_id == 0){
$sql = "SELECT * FROM categories WHERE parent_id IS NULL";
} else {
$sql = "SELECT * FROM categories WHERE parent_id =:parentid";
}
$stmt = $this->db->prepare($sql);
$stmt->bindParam(':parentid', $parent_id);
$stmt->execute();
$categories = array();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
array_push($categories, array($row['id'] => $row['name']));
}
return $categories;
}
Here's my category page:
<?php
//Instantiate categories class
$categories = new categories($db);
$categoriesMain = $categories->showCategories(0);
?>
<html>
<head></head>
<body>
<form action="" method="post">
<?php //Get parent categories and put them into a select box ?>
<select name="categoriesMain">
<?php for($i=0;$i<count($categoriesMain);$i++){ ?>
<option value="<?php echo $i; ?>">
<?php echo $categoriesMain[$i]; ?>
</option>
<?php } ?>
</select>
<input type="submit" name="submit" value="submit"/>
</form>
<?php //if form submits then show sub categories ?>
<?php if(isset($_POST['submit'])){
$categoriesSub = $categories->showCategories($_POST['categoriesMain']);
for($i=0;$i<count($categoriesSub);$i++){
echo $categoriesSub[$i];
}
} ?>
</body>
</html>
Let me try to explain what im having trouble with. I think my whole design is out of place because it feels like that but im having a brain block at the moment.
In the function I'm returning an array like Array ( [0] => Array ( [1] => Computers ) [1] => Array ( [4] => Music ) ). If you think this is the wrong way to return it let me know. Ok then do you see CategoriesMain? I'm using a for loop to output this array and for option=value im echoing $i however this $i goes like 1, 2, 3, 4 however I want the value to be the value of the parent category e.g. 1, 4 so that I can collect the value using $_POST['cateogoriesMain'] in the next for loop where I'm displaying cateogriesSub where it will get the database rows for those with the parent_id = to whatever was selected in the selectbox previously. I hope it makes sense.
You should use the key of the array for the option value, like this:
<select name="categoriesMain">
<?php foreach ($categoriesMain as $k => $v) { ?>
<option value="<?php echo $k; ?>">
<?php echo $v; ?>
</option>
<?php } ?>
</select>
edit also change the following line inside the php function, instead of:
array_push($categories, array($row['id'] => $row['name']));
do
$categories[$row['id']] = $row['name'];