I can't get the input of checkboxes in PHP.
Here is my code:
echo "<table>";
while ($zeile = mysqli_fetch_array( $ergebnis, MYSQLI_ASSOC )){
echo "<tr>";
echo "<td> <input type='checkbox' name='check_list[]' id='".$zeile['AGName']."'/> </td>";
echo "<td>". $zeile['AGName'] . "</td>";
echo "</tr>";
}
echo "</table>";
#this is the part that probably isn't correct.
if(!empty($_POST['check_list'])){
$checked_count = count($_POST['check_list']);
echo "You have selected following ".$checked_count." option(s): <br/>";
}
I would like to get the amount of checkboxes checked.
The checkboxes are created in a loop with the input of a database.
Even if this would work, how would I get the id of all of the checked checkboxes?
It seems you are not using the form to submit. Place your table inside the form
<form action="" method="post">
<?php
echo "<table>";
while ($zeile = mysqli_fetch_array( $ergebnis, MYSQLI_ASSOC )){
echo "<tr>";
echo "<td> <input type='checkbox' name='check_list[]' value='".$zeile['AGName']."'/> </td>";
echo "<td>". $zeile['AGName'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</form>
You can get the post values
if($_POST){
$checked_count = count($_POST['check_list']);
echo "You have selected following ".$checked_count." option(s): <br/>";
}
Simple form with checkboxes:-
<form name="" action="" method="post">
<input type="checkbox" name="gender[]" value="Male" />Male
<input type="checkbox" name="gender[]" value="Female" />Female
<input type="submit" name="submit" value="Submit" />
</form>
The PHP code to get the selected:-=
if(isset($_POST['gender'])){
$options = $_POST['gender'];
echo implode(',', $options);
}
If you want to pass the id you can do it like
<input type="checkbox" name="gender[2]" value="Male" />Male
<input type="checkbox" name="gender[3]" value="Female" />Female
You can loop through each option
foreach($options as $key => $value){
echo $key.'---'.$value;
}
//$key is the id sepcified, $values is the seected value
Related
<?php
if(isset($_POST['feature'])){
if(!empty($_POST['test'])){
foreach($_POST['test'] as $selected){
var_dump($_POST['test']);
}
}
}
?>
<form action="" method="POST">
<?php
$query = $products->findAll();
foreach($query as $row){
if($row['featured'] == 'Yes'){
echo "<label for='featured'>" . $row['p_name'] . " </label> <input type='checkbox' name='test[]' value = 'Yes' checked>";
}
else
{
echo "<label for='featured'>" . $row['p_name'] . " </label> <input type='checkbox' name='test[]' value ='No'>";
}
}
?>
<input type="submit" name="feature" value="Feature">
</form>
I have 5 products which is 'selected'. When I loop through to find what is selected I get 5 different arrays with 5 strings? I cant see where I've gone wrong, it should return 5 array's with just the one string of 'Yes' to show it's selected?
You are dumping the full array. You want:
var_dump($selected);
I am creating a form and in the form, all the input fields will display dynamically. I am able to create the text, email, password, checkbox but not able to create the select and radio. I have to use the select tag for country and radio for gender.
I am using CodeIgniter.
<?php
$example_from_name = array('name' => 'user_from_view'); //assigning form name
echo form_open('formbuilder_control/example_from_view',$example_from_name);
foreach ($data as $key) {
$exp_fields_name=$key->fields_name; //here I am getting the name of field name
$exp_fields_type=$key->fields_type; //here I am getting the input type
?>
<div class="form-group row label-capitals">
<label class="col-sm-5 col-form-label"><?php echo $exp_fields_name;?></label>
<div class="col-sm-7">
<input type="<?php echo $exp_fields_type;?>" name="<?php echo $exp_fields_name;?>" placeholder="<?php echo $exp_fields_name;?>" class="form-control" />
<?php echo form_error($exp_fields_name); ?>
</div>
</div>
<?php
}
?>
<div class="form-buttons-w btn_strip">
<input type="submit" value="Save" class="btn btn-primary margin-10">
</div>
<?php echo form_close(); ?>
You don't have enough dynamic data for some of the fields. You will have to pass more from your database. A few of the fields will require values (select, radio, checkbox) Here is the basic structure (as the others were trying to say):
<?php
$example_from_name = array('name' => 'user_from_view'); //assigning form name
echo form_open('formbuilder_control/example_from_view',$example_from_name);
foreach ($data as $key) {
$exp_fields_name=$key->fields_name; //here I am getting the name of field name
$exp_fields_type=$key->fields_type; //here I am getting the input type
echo "<div class=\"form-group row label-capitals\">";
echo "<label class=\"col-sm-5 col-form-label\">$exp_fields_name</label>";
echo "<div class=\"col-sm-7\">";
if(in_array($exp_fields_type,['text','email','password'])){
echo "<input type=\"$exp_fields_type\" name=\"$exp_fields_name\" class=\"form-control\" />";
}elseif($exp_fields_type=='checkbox'){
echo "<input type=\"$exp_fields_type\" name=\"$exp_fields_name\" value=\"[something needed here]\" class=\"form-control\" /> $exp_fields_name";
}elseif($exp_fields_type=='select'){
echo "<select name=\"$exp_fields_name\">";
echo "<option></option>"; // you will have to determine a way to populate the options
// ... more options
echo "</select>";
}elseif($exp_fields_type=='radio'){
echo "$exp_fields_name <input type=\"$exp_fields_type\" name=\"$exp_fields_name\" value=\"[something needed here]\" class=\"form-control\" />";
}else{
echo "Whoops, uncaught field type!";
}
echo form_error($exp_fields_name);
echo "</div>";
echo "</div>";
}
echo "<div class=\"form-buttons-w btn_strip\">";
echo "<input type=\"submit\" value=\"Save\" class=\"btn btn-primary margin-10\">";
echo "</div>";
echo form_close();
We, volunteers, cannot help you with the missing components of your design. You will need to figure out how you are going to pass the necessary options/values to the necessary html form elements.
If you have known field names, you might try writing the conditionals using $exp_fields_name instead of $exp_fields_type to cover a few exceptional cases. Perhaps exchange the above if-block with the following:
if($exp_fields_name=='gender'){
echo "<input type=\"radio\" name=\"gender\" value=\"male\" class=\"form-control\" /> Male<br>";
echo "<input type=\"radio\" name=\"gender\" value=\"female\" class=\"form-control\" /> Female";
}elseif($exp_fields_name=='country'){
echo "<select name=\"country\" class=\"form-control\" />";
echo "<option>India</option>";
echo "<option>Sri Lanka</option>";
echo "<option>Japan</option>";
// ...continue as needed
echo "</select>";
}else{ // all other types default to input tag
echo "<input type=\"$exp_fields_type\" name=\"$exp_fields_name\" class=\"form-control\" />";
}
I assume "checkbox" inputs will have multiple inputs, so you may need to write a custom check for that group as well.
you have to use IF or switch using this type:
<?php
if($exp_fields_type == 'text'){
$input = '<input type="text" name="'.$exp_fields_name;.'" value="" placeholder="" />';
}else if($exp_fields_type == 'radio'){
$input = '<input type="radio" name="'.$exp_fields_name;.'" value="" placeholder="" />';
}
?>
Your code will works only for input tags. So you should check the type before creating the tag
for example :
<?php
if($exp_fields_type == 'select'){
$input = '<select name="'.$exp_fields_name.'" placeholder="" >
<option value="">option 1 </option>
</select>';
}else if($exp_fields_type == 'textarea'){
$input = '<textarea name="'.$exp_fields_name.'"></textarea>';
}else {
$input = '<input type="'.$exp_fields_type.'" name="'.$exp_fields_name.'" value="" placeholder="" />';
}
?>
function LoadProtocols(){
?>
<?php
$result=mysql_query("SELECT id,name,Destination_port, Destination_port_range_start, Destination_port_range_stop FROM protocols;");
echo '<table class="tftable" border="1">';
echo '<tr><th>protocol Name</th><th>Port</th><th></th></tr>';
echo "<form method='POST' action=''>";
if( $row=mysql_fetch_array($result)){
mysql_data_seek($result,0);
while( $row=mysql_fetch_array($result))
{
echo "<tr>
<td>".$row['name']."</td>
<td> "?><input type="hidden" id="id" name="id[]" value=" <?php echo $row['id']?>"> <input id="txt_port" name="txt_port[]" type="text" class="required" title="Port. This is a required field" value=" <?php echo $row['Destination_port'] ?> "></td>
<?php
echo "<td><a onclick='return show_confirm();'><input name='update' class='button' type='submit' value='Update'></a></td></tr>";
if(ISSET($_POST["update"]) and $_SERVER['REQUEST_METHOD'] == "POST"){
$id=$_REQUEST['id'];
$port=$_REQUEST['txt_port'];
$menu=$_GET['Menu'];
$result=mysql_query("UPDATE protocols SET Destination_port=".$port." WHERE id=".$id.";");
header("Location: Overview.php?Menu=".$menu."&Overview=1");
}
echo "</form>";
}
echo '<table>';
}
I made a simple table which as got 3 columns, the first column is a name, second one has a textbox which contains a number (with a hidden id that I need) and last one is a button to update that specific row.
The big problem I have that it only updates the last row, therefore I read that you needed to add [] to ur names and loop through it to update that specific row.
It's still not working and I can't figure out how to just let one specific row update with that [] array.
Thank you for reading and helping.
$_request['id'] is nothing but array ,also header should not be in loop
so Write This
mysql_data_seek($result,0);
$counter=0;
while( $row=mysql_fetch_array($result))
{
echo "<tr>
<td>".$row['name']."</td>
<td> "?><input type="hidden" id="id" name="id[]" value=" <?php echo $row['id']?>"> <input id="txt_port" name="txt_port[]" type="text" class="required" title="Port. This is a required field" value=" <?php echo $row['Destination_port'] ?> "></td>
<?php
echo "<td><a onclick='return show_confirm();'><input name='update' class='button' type='submit' value='Update'></a></td></tr>";
if(ISSET($_POST["update"]) and $_SERVER['REQUEST_METHOD'] == "POST"){
$id=$_REQUEST['id'];
$port=$_REQUEST['txt_port'];
$menu=$_GET['Menu'];
$result=mysql_query("UPDATE protocols SET Destination_port=".$port." WHERE id=".$id[$counter].";");
$counter++;
// header("Location: Overview.php?Menu=".$menu."&Overview=1");
}
Instead of this
mysql_data_seek($result,0);
while( $row=mysql_fetch_array($result))
{
echo "<tr>
<td>".$row['name']."</td>
<td> "?><input type="hidden" id="id" name="id[]" value=" <?php echo $row['id']?>"> <input id="txt_port" name="txt_port[]" type="text" class="required" title="Port. This is a required field" value=" <?php echo $row['Destination_port'] ?> "></td>
<?php
echo "<td><a onclick='return show_confirm();'><input name='update' class='button' type='submit' value='Update'></a></td></tr>";
if(ISSET($_POST["update"]) and $_SERVER['REQUEST_METHOD'] == "POST")
{
$id=$_REQUEST['id'];
$port=$_REQUEST['txt_port'];
$menu=$_GET['Menu'];
$result=mysql_query("UPDATE protocols SET Destination_port=".$port." WHERE id=".$id.";");
header("Location: Overview.php?Menu=".$menu."&Overview=1");
}
Im fetching data from database table when i m trying to delete a particular record no matter what i click last record get deleted what m i doing wrong, this is code m using after submit/click del button
// DELETE
if(isset($_POST['del']))
{
require'conn.php';
$delete_id = $_POST['del_id'];
print_r($_POST);
die;
$del_stmt = "DELETE FROM signup WHERE ID =$delete_id";
mysqli_query($conn,$del_stmt);
mysqli_execute($del_stmt);
$row=mysqli_affected_rows($conn);
if($row==1)
{
echo "<h1>".' sucess ! record was deleted' ."</h1>";
}
else
{
echo "<h1>".' record was not deleted '."</h1>";
}
mysqli_close($conn);
}
include'fetchtable.php';
and this is my table structure and del button code
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
<?php
echo "<table border='1' cellpadding='2' cellspacing='2'";
echo "<tr><td>ID</td><td>First Name</td><td>Last Name</td><td>Gender</td><td>Email</td><td>Password</td><td>Delete</td><td>Edit</td>";
while (mysqli_stmt_fetch($stmt))
{
echo"<tr>";
echo "<td>".$id."</td>";
echo "<td>". "$fn" ."</td>";
echo "<td>". "$ln" ."</td>";
echo "<td>". "$gen"."</td>";
echo "<td>". "$email"."</td>";
echo "<td>". "$pass" ."</td>";
echo '<td> <input type="hidden" name="del_id" value="'.$id.'" />'. '<input type="submit" name="del" value="delete" /> ';
echo '<td> <input type="hidden" name="edit_id" value="'.$id.'" />'.' <input type="submit" name="edit" value="edit" /> ';
echo"</tr>";
}
?>
</form>
As Abhik Chakraborty says, you need a form for each row, or an other logic;
one solution is to put the <form ...>...</form> within the loop:
this is - AFAIK - not correct, in does not conform to HTML std. and works only with some browsers, bcause <table> and <form> ar mixed in wrong order, I use it only as egsample to show you problem
<?php
echo "<table border='1' cellpadding='2' cellspacing='2'";
echo "<tr><td>ID</td><td>First Name</td><td>Last Name</td><td>Gender</td><td>Email</td><td>Password</td><td>Delete</td><td>Edit</td>";
while (mysqli_stmt_fetch($stmt))
{
echo "<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">"
echo"<tr>";
echo "<td>".$id."</td>";
echo "<td>". "$fn" ."</td>";
echo "<td>". "$ln" ."</td>";
echo "<td>". "$gen"."</td>";
echo "<td>". "$email"."</td>";
echo "<td>". "$pass" ."</td>";
echo '<td> <input type="hidden" name="del_id" value="'.$id.'" />'. '<input type="submit" name="del" value="delete" /> ';
echo '<td> <input type="hidden" name="edit_id" value="'.$id.'" />'.' <input type="submit" name="edit" value="edit" /> ';
echo"</tr>";
echo "</form>"
I would preferre to have only one hidden field in the form und to set the value of that field with onClick event of the submit button.
only the last lines again:
...
echo '<td> <input type="submit" name="del" value="delete" onclick="form.row_id.value='$the id$';"/>'
echo '<td> <input type="submit" name="edit" value="edit" onclick="form.row_id.value='$the id$';"/>'
echo"</tr>";
}
?>
<input ID='row_id' type="hidden" name="del_id" value="no set till now" />'
</form>
You need to have multiple for each action and so you need to have the code as
while (mysqli_stmt_fetch($stmt)){
echo '<form action="'.htmlspecialchars($_SERVER["PHP_SELF"]).'" method="post">' ;
echo "<tr>";
echo "<td>".$id."</td>";
echo "<td>". "$fn" ."</td>";
echo "<td>". "$ln" ."</td>";
echo "<td>". "$gen"."</td>";
echo "<td>". "$email"."</td>";
echo "<td>". "$pass" ."</td>";
echo '<td> <input type="hidden" name="del_id" value="'.$id.'" />'. '<input type="submit" name="del" value="delete" /> ';
echo '<td> <input type="hidden" name="edit_id" value="'.$id.'" />'.' <input type="submit" name="edit" value="edit" /> ';
echo "</tr>";
echo "</form>";
}
Also your code is not safe, you need to use mysqli_real_escape_string() for your POST data. or prepared statement
I need to get values of database in text boxes on button click using PHP and Javascript. For instance, I get values in an HTML table from a database table. I need to get the respective values in the text boxes when the user clicks on the add0 button.
Here is my code:
<form method="post" action="">
<input type="text" name="tb1" />
<input type="text" name="tb2" />
<input type="submit" name="btn" value="Find" />
</form>
<?php
include "conn.php";
$show = "SELECT * FROM data";
$rs = mysql_query($show) or die(mysql_error());
$add_to_textbox = "<input type='button' name='btn' value='add0' />";
#****results in Grid****
echo "<table width='360px' border='1' cellpadding='2'>";
while($row = mysql_fetch_array($rs)) {
echo "<tr>";
echo "<td width='130px'>$row[Name]</td>";
echo "<td width='230px'><a href = '$row[Link]'>$row[Link]</a></td>";
echo "<td width='130px'>$add_to_textbox</td>";
echo "</tr>";
}
echo "</table>";
#**********************
mysql_free_result($rs);
?>
I need further code on button click.
imho you can use Inline edit using Ajax in Jquery
Here is it's demo
It will let you edit your displayed contents in the table itself..
Update:
<form method="post" action="">
<input type="text" name="tb1" id="tb1" />
<input type="text" name="tb2" id ="tb2" />
<input type="submit" name="btn" value="Find" />
</form>
<?php
include "conn.php";
$show = "SELECT * FROM data";
$rs = mysql_query($show) or die(mysql_error());
$add_to_textbox = "<input type='button' name='btn' value='add0' />";
#****results in Grid****
echo "<table width='360px' border='1' cellpadding='2'>";
$rowID=1;
while($row = mysql_fetch_array($rs)) {
echo "<tr>";
echo "<td width='130px' id='name".$rowID."'>$row[Name]</td>";
echo "<td width='230px' id='link".$rowID."'><a href = '$row[Link]'>$row[Link]</a></td>";
echo "<td width='130px' onclick='txtValDisp($rowID);'>$add_to_textbox</td>";
echo "</tr>";
$rowID++;
}
echo "</table>";
#**********************
mysql_free_result($rs);
?>
<script type="text/javascript">
function txtValDisp(rowID){
var linkVal = document.getElementById('link'+rowID+'').innerHTML.replace(/<\/?[^>]+(>|$)/g, "\n");
document.getElementById("tb1").value = document.getElementById('name'+rowID+'').innerHTML;
document.getElementById("tb2").value = linkVal;
}
</script>
Recreate your form with default values taken from the database.
<form method="post" action="">
<input type="text" name="tb1" />
<input type="text" name="tb2" />
<input type="submit" name="btn" value="Find" />
</form>
<?php
include "conn.php";
$show = "SELECT * FROM data";
$rs = mysql_query($show) or die(mysql_error());
$add_to_textbox = "<input type='button' name='btn' value='add0' />";
#****results in Grid****
echo "<table width='360px' border='1' cellpadding='2'>";
while($row = mysql_fetch_array($rs)) {
echo "<tr>";
echo "<td><input name ='INSERT_HERE' type=text value='"$row[Name]"'></td>";
echo "</tr>";
}
echo "</table>";
#**********************
mysql_free_result($rs);
?>
You just need to change the name of the object based on whatever counter of something...