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 :)
Related
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.
<form action="addbusstop.php" method="POST" id="produceJSON" enctype="multipart/form-data">
<?php
echo "<div class = 'dropdown_container'><select class = 'dropdown select xs-mt1' name = 'line' id = 'line'>";
foreach($ptv_json_bus as $item)
{
$stopid = $item['stop_id'];
$stopname = $item['location_name'];
?>
<option value = "<?php echo $stopid;?>"> <?php echo $stopname;?></option>
<?php
}//end foreach
?>
<input type = "hidden" value ="<?php echo $stopid; ?>" name="stop_id" id="stop_id">
<input type = "hidden" value ="<?php echo $stopname; ?>" name="stop_name" id="stop_name">
</select>
<button type="submit" class="button button-latrobe-orange xs-ml2" onclick="changeState3()" id="submitstop2" name="submitstop2">OK</button>
</div>
</form>
Hey guys. Above is the code I'm working with.
What I am trying to achieve is to POST the selected value's stop ID and stop NAME (from a dropdown) to another file which then adds the data into mySQL.
What my problem is right now is that the current code I have is actually only POSTing the last set value of $stopid and $stopname which is not what I need.
I've been stuck on this for quite a while and I would love it it somebody here could point me in the right direction!!
Thank you
Move your <input> elements outside of the <select> element.
You can only put <option> elements inside of your <select> element.
I've improved your version a bit.
<form action="addbusstop.php" method="POST" id="produceJSON" enctype="multipart/form-data">
<div class = 'dropdown_container'>
<select class="dropdown select xs-mt1" name="line" id="line">
<?php
foreach ($ptv_json_bus as $item) {
$stopid = $item['stop_id'];
$stopname = $item['location_name'];
echo "<option value='$stopid'>$stopname</option>";
}
?>
</select>
<input type="hidden" value="<?php echo $stopid; ?>" name="stop_id" id="stop_id">
<input type="hidden" value="<?php echo $stopname; ?>" name="stop_name" id="stop_name">
<button type="submit" class="button button-latrobe-orange xs-ml2" onclick="changeState3()" id="submitstop2" name="submitstop2">OK</button>
</div>
</form>
Hello i want to use multiple input type text and same checkboxes along with them to access values by post request in php but problem is if i check the checkbox it will show one item respected to checkbox but it shows all elements of input type text from foreach
I have used :
<input type="text" name="nam[]" value="xyz">
<input type="checkbox" name="chk[]" value="xyz1">
Same with10 inputs.
It's work with inputs, but how can i acheive the same with checkboxs?
Hope this code will help you:
form.php page:
<form method="POST" action="process.php">
<table>
<?php
for($i=0;$i<10;$i++){
echo '<tr><td><input type="text" name="nam['.$i.']" value="input'.($i+1).'"></td>';
echo '<td><input type="checkbox" name="chk['.$i.']" value="checkbox'.($i+1).'"></td></tr>';
}
?>
<tr><td><input type="submit"></td><td></td></tr>
</table>
process.php
<?php
foreach($_POST['chk'] as $key=>$value){
echo 'Checkbox value = '.$value.' and input value = '.$_POST['nam'][$key].'<hr />';
}
?>
I have a dynamic number of checkbox in my view,like this:
<?php foreach($documents as $row): ?>
<input type="checkbox" name="options[]" value="<?php echo $row->docu_title?>"><?php echo $row->docu_title?><?php endforeach; ?>
And I set the rule for this group of checkboxes to be required in my controller:
$this->form_validation->set_rules('options[]','options', 'required');
How will i know which checkboxes are checked? so whenever there are errors on the other fields i can still show the user the checkboxes that has been checked already. like this:
<input style="" type="text" class="form-control" name="ClientName" id="ClientName" value="<?php echo set_value('ClientName'); ?>">
You could use form helper 's set_checkbox() function.
This permits you to display a checkbox in the state it was submitted. The first parameter must contain the name of the checkbox, the second parameter must contain its value, and the third (optional) parameter lets you set an item as the default (use boolean TRUE/FALSE). Example:
<input style="" type="checkbox" class="form-control" name="ClientName" id="ClientName"
value="<?php echo set_value('ClientName'); ?>" <?php echo set_checkbox('ClientName', '1'); ?> />
For reference visit CodeIgniter User Guide Version 2.2.0
should be something like this
<?php foreach($documents as $row): ?>
<input type="checkbox" name="option[]" value="<?php echo $row->docu_title?>" <?php echo set_checkbox('option[]', $row->docu_title); ?>>
<?php echo $row->docu_title?>
<?php endforeach; ?>
I tried $_POST['<?php echo $var ?>] but I should have known that it wouldn't be that easy.
The reason why I try to do is because I have several input boxes with values I take from a database and I'm trying to create an updation script where any of the input box values can be changed.
for example
<form action="process.php" method="post">
<?php
while($variable=mysql_fetch_array($sqlconnec))
{
?>
<input type="text" name="<?php echo $variable['col1']?>" value="<?php echo $variable['val'] ?>" />
<?php
}
?>
<input type="submit" />
</form>
Any help is appreciated.
I think that what you need is:
<input type="text" name="<?php echo $col; ?>" value="<?php echo $val; ?>" />
$_POST[$col] //this will have the input value defined above.
In process.php you have to do the same query as mentioned above. If you iterate through those results $_POST[$col] will contain the posted values.
You need to do like this:
<form action="process.php" method="post">
<?php
$variable = mysql_fetch_assoc($sqlconnec);
foreach($variable as $col => $val)
{
?>
<input type="text" name="<?php echo $col; ?>" value="<?php echo $val; ?>" />
<?php
}
?>
<input type="submit" />
</form>
Now, mysql_fetch_assoc gets you the database row in a associative array. Then, the code iterates each column in the row and displays the name/value pair for it. And yes, you were not closing the value tag correctly.
foreach($_POST as $k=>$v) {
//do something with $v or $_POST[$k]
}
I think that you want to change the name of the input to something that is constant.
For example:
<input type="text" name="testname" value="<?php echo $variable['val'] ? />
And then retrieve your variable like so:
$_POST['testname']
For example you could print the variable you sent in the input to test it like so:
echo $_POST['testname'];
You are not closing your input 'value' tag with ". Also your second php closing tag is incorrect.
<input type="text" name="<?php echo $variable['col1']?>" value="<?php echo $variable['val'] ?>" />