Retrieving table field when checkbox values are equal to another field - php

I have language table that include LID,LNAME fields in a UI.
I have many checkboxes for LNAME.
When the user checks some of them and submits, the system display LID for the checked checkbox.
My HTML:
<input type ="checkbox" name="language[]" vaue="English">English
In PHP:
$lang=$_POST['language'];
$sql="select LID from lqnguage where lname=$lang";
Please can anyone help me please I'm having trouble.

Try below code.
PHP
// This is array for language.
$l_array=array();
$l_array[1]='Gujarati';
$l_array[2]='English';
$l_array[3]='Spenish';
// Get selected languages.
if(isset($_POST['submit']) && $_POST['submit']=='Submit'){
echo "<pre>";
print_r($_POST['language']);
}
HTML
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<?php foreach($l_array as $lid=>$lname){ ?>
<input type="checkbox" name="language[]" value="<?php echo $lid; ?>" /><?php echo $lname; ?><br />
<?php } ?>
<input type="submit" name="submit" value="Submit" />
</form>
Now, you can get selected language_id(as key) and language_name(as value) in Array.

Related

how to select multiple rows in table via checkbox and pass these values to another table

I want to select multiple rows in a table via checkboxes and store these values in another table.I have almost done. But problem is that when I select multiple rows and get these values via array. It only fetch single value. I want all selected values. Please help me in this regard. My code is given below.
<form class="form-horizontal" method="post" action="add_exam_quiz.php" role="form" id="form1">
<tr>
<td>
<input type="checkbox" name="quiz_question[<?php echo $question_no; ?>]" value="<?php echo $question_no; ?>">
<?php echo $question_no; ?>
</td>
</form>
</tr>
<input class="btn btn-primary" type="submit" name="submit" value="submit" form="form1">
I expect an array with all checked values. But I got single value.
You need to have all the checkboxes with the same name like this:
<td><input type="checkbox" name="quiz_question[]" value="<?php echo $question_no; ?>"> <?php echo $question_no; ?> </td>
then in PHP:
<?php
$quiz_question = $_POST['quiz_question'];
foreach ($quiz_question as $question=>$value) {
echo $value."<br />";
}
?>
Try to give same NAME for all check box options. If you give different NAME it won't work.

Passing Input values from one PHP page to another within hidden fields

$sql="SELECT vName,id FROM employee WHERE vName LIKE '%$my_data%' ORDER BY vName";
$result = mysql_query($sql);
if($result)
{
while($row=mysqli_fetch_array($result))
$hid='<input type="hidden" name="xyz" id="abc" value="'.$row['id'].'" />';
echo($hid);
echo $row['vName']."\n";
}
How to pass the value of a hidden input field to another PHP script? I am using auto complete. how to pass the value auto complete page to index page
You have two options:
Sessions
PHP Sessions
Session support in PHP consists of a way to preserve certain data across subsequent accesses.
eg:
<?php
// Page1.php
session_start();
$_SESSION["key"] = "random value";
Then:
<?php
// Page2.php
session_start();
echo $_SESSION["key"];
// Output would then be ... random value
POST
Using the PHP $_POST
Taking what you currently have, you'd do:
<form method="post" action="somescript.php">
<input type="hidden" name="xyz" id="abc" value="<?=$row['id'] ?>" />
<button type="submit" name="submit" value="submitForm" />
</form>
Then on somescript.php if you do:
<?php
print_r($_POST);
You'll see an array with the data from your form, hidden value included
Create a form
<form action="action_page.php" method="get">
<input type="hidden" name="xyz" id="abc" value="'.$row['id'].'" />
<input type="submit" value="Submit">
</form>
And Get value on action_page.php
$_GET['xyz']
You enter your html code inside php code like this
<?php
while($row=mysqli_fetch_array($result))
{
?>
<form action="action_page.php" method="get">
<input type="hidden" name="xyz" id="abc" value="'.$row['id'].'" />
<input type="submit" value="Submit">
</form>
<?php
echo $row['vName']."\n";
}
?>

Three page sequence of forms (checkboxes and text/number boxes) with foreach in php

I would like to pass variables through 3 pages. The 1st page asks the user which music genres they like (there will eventually be 20+ genres). The 2nd page asks the user to rank the genres they have selected and the 3rd page sorts and displays their ranking.
This first page asked the user to pick which genres they like:
<form id="genre" name="genre" method="post" action="musicsell.php">
<input type="checkbox" name="genre[]" value="Rap"/>Rap<br />
<input type="checkbox" name="genre[]" value="HipHop"/>HipHop<br />
<input type="checkbox" name="genre[]" value="RnB"/>RnB<br />
<input type="checkbox" name="genre[]" value="Rock"/>Rock<br />
<input type="checkbox" name="genre[]" value="Jazz"/>Jazz<br />
<p>
<input type="submit" value="Next">
<br />
</p>
</form>
This second asks them to rank (prioritize) the genres they have selected with 1 being the best:
<body>
The genre(s) you selected are: <br>
<form id="form1" name="form1" method="post" action="musicresults.php">
<?php
$name = $_POST['genre'];
if(isset($_POST['genre'])) {
foreach ($name as $genre){
?>
<input
type="number" required="required" id="<?php echo $genre ?>" name="music[<?php echo $genre ?>]" />
<?php echo $genre ?><br />
<?php
}
}
?>
<input type="submit" name="button" id="button" value="Submit" /></form>
</body>
The third and last page sorts and echos the results:
<?php
//Get the form results (which has been converted to an associative array) from the $_POST super global
$musicgenres = $_POST['music'];
//Sort the values by rank and keep the key associations.
asort($musicgenres, SORT_NUMERIC );
//Loop over the array in rank order to print out the values.
foreach($musicgenres as $music => $rank)
{
echo "$musicgenres is your $rank choice";
echo "<br>";
}
?>
It all works fine until the last page where I'm getting an "array to string conversion" error. Maybe I need to put in session variables but I'm not sure.
Any help would be appreciated.
It's exactly what the error says. It can't convert and array to a string.
Replace
echo "$musicgenres is your $rank choice";
with
echo "$music is your $rank choice";

How to use GET to retrieve check box selected value in php

I am using Mysql PDO query to fetch the required result and it is saved in and array $newfamily. Now with the help of this array I am implementing check-box with the given code-
<form method="get" action="specific_family.php">
<?php
foreach($newfamily as $name)
{
?>
<input type='checkbox'<?php echo $name;?>"><?php echo $name;?></input>
<?php
}
?>
</select> <input type = "submit" name="submit" >
</form>
Now in specific_family.php how can I retrieve al the selected check-box values in an array ?
Also when I use back button of browser I can see previously selected values as ticked. How can I remove this ?
Please guide.
The checkbox should have:
A label
The checkbox needs:
A name attribute
A value attribute
It must not have:
An end tag (unless you are using XHTML)
So:
<label>
<input type="checkbox"
name="new_families[]"
value="<?php echo htmlspecialchars($name); ?>">
<?php echo htmlspecialchars($name); ?>
</label>
The values of all the checked checkboxes will then appear in the array $_GET['new_families'] when the form is submitted.
If you add the name attribute to your input thus:
<form method="get" action="specific_family.php">
<?php
foreach($newfamily as $name)
{
?>
<label for="<?php echo $name?>"><?php echo $name?></label>
<input type='checkbox' name="<?php echo $name;?>" id="<?php echo $name;?>"></input>
<?php
}
?>
</select> <input type = "submit" name="submit" >
</form>
then your checkboxes will show up by name in your $_GET array.
Hope that helps :)

how can i put the value of "value" field in a form where input type is checkbox from database?

i tried alot in order to fetch values from database and put it in check-box array but failed.Please help!!
*img[] comes out to be empty all the time like no value is going into it when ever i call that part using $_POST(['img'])!!*
here's my code:
echo " <h2>Select image to delete: <h2>";
$s = mysql_query("SELECT * FROM image WHERE u_id = '$u_id'");
$num = mysql_num_rows($s);
if($s)
{
?>
<form name="f1" method="post" action="">
<?php
while($row = mysql_fetch_array($s))
{
?>
<input type="checkbox" name="img[]" value="<?php $row['path'] ;?>" />
<img width="100" src="<?php echo $row['path']." ";?>">
<?php
}
?>
<br />
<br />
<input type="submit" name= "subDel" value = "Delete" />
</form>
<?php
}
What about this? :)
<input type="checkbox" name="img[]" value="<?php $row['path'] ;?>" />
=>
<input type="checkbox" name="img[]" value="<?php ECHO $row['path'] ;?>" />
Retrive post Data in PHP by using $_POST['variableName'] - no brackets, case DOES matter
In your case, $_POST['img'] is of type array. There is NO value for unchecked items. If nothing checked, your post variable is empty or even undefined (didn't test it yet).
Access your values by
if (array_key_exists($index, $_POST['img']) == true) {
// $index is checked
$doSomething = $_POST['img'][$index];
}

Categories