how to pass value of checked checkbox in php - php

Hi i am generating checkbox list by using a for each loop, I want to pass the value of checked checkbox into database.
foreach($this->lis as $lst)
{?>
<tr>
<td>
<input type="checkbox" name="list" value="1" />
<label for="list_32"><?php echo $list->nList ?></label>
</td>
</tr>
<?php } ?>
when i checked the checkbox i want to pass the label name into the database can someone help.

Check this code. You need to take array as checkbox name i.e. list[]. Once form is submitted you can use it's value to insert in to database.
<?
if(isset($_POST['submit'])) // once form is submitted build your logic
{
$list = $_POST['list'];
foreach($list as $value)
{
echo "<br />Checked: $value";
// use $value to insert into database
$sql = "insert into...";
mysql_query($sql);
}
}
?>
<form name="frm" method="post">
<table>
<? foreach($this->lis as $lst)
{?>
<tr>
<td>
<input type="checkbox" name="list[]" value="<?php echo $list->nList ?>" />
<label for="list_32"><?php echo $list->nList ?></label>
</td>
</tr>
<?php } ?>
<tr>
<td><input name="submit" value="Submit" type="submit" /></td></tr></table>
</form>

Ok, you've got a few problems there..
Firstly, please avoid opening php tags and closing them constantly. Just echo back the HTML. Going in and out of php so often will cause performance issues.
So, first off, you need to set up a form on here, your form needs to point to a second page (or self-process using an isset function, but let's go with the former solution for clarity). The second page is what's going to dump things into the DB for you based on what the user ticked.
So, here's the HTML for your form;
<form action="database.php" method="POST">
<div>
<label for="checkbox1">This is the first option:</label>
<input type="checkbox" id="checkbox1" name="checkbox1" />
</div>
<div>
<label for="checkbox2">This is the second option:</label>
<input type="checkbox" id="checkbox2" name="checkbox2" />
</div>
<div>
<input type="submit" />
</div>
</form>
Again, I'd recommend echo'ing this back in your foreach loop - a short function could accomplish this for you.
So, once someone hits the "Submit" button, they'll be taken to your second page (database.php as we've called it), and their options will be stored in the POST array. For good practice, always use POST rather than GET when dealing with DB entries.
So on this page, we'd check what checkboxes were selected, and then update the db as required;
if (isset($_POST['checkbox1']))
{
// user ticked checkbox1
$sql = "UPDATE table
SET 'checkbox1' = 1
WHERE 'user' = 'username';"
mysqli_query($sql);
}
Note that's just a rough solution - you haven't told me exactly how you're doing this, so I'm assuming your DB is tied to user's votes etc. You can also update checkbox1 by incrementing the value.. etc etc.
Hope this helps.
Eoghan

In the name you should use [] (because it's an array). So in this case it'd be name="list[]". Then when you submit it (either post or get) you just get the checked data on the list[] array. I'm not sure if I understood your question. If I didn't; please share a little more information.

I think this is what you're looking for:
foreach($this->lis as $lst)
{?>
<tr>
<td>
<input type="checkbox" name="list[<?php echo $list->nList ?>]" value="1" />
<label for="list_32"><?php echo $list->nList ?></label>
</td>
</tr>
<?php } ?>
Then in PHP, you would do foreach($_POST['list'] as $listval) to loop over it again. Substitute $_POST for $_GET if you are using GET in your form.
Edit: I just noticed where you did echo $list->nList that this variable is either not going to change in your loop or it's a typo. Assuming it's a typo, it should be echo $lst->nList instead. If it's not a typo, then you want to just remove it and have name="list[]" in your HTML

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.

Get the value of clicked text box from array of text boxes using PHP

i want to get the text box value which i selected from array of text boxes..
My code is like this:
Here am displaying the data on the browser from DB..
<?php
foreach($dataDecoded['categories'] as $key => $value)
{
?>
<tr>
<td><?php echo $value['CategoryName'];?></td>
<td>
<input type="text" name="categoryId[]" id="categoryId" value="<?php echo $value['CategoryId']?>">
<input type="text" name="categoryName[]" id="categoryName" value="<?php echo $value['CategoryName']?>">
<input type="submit" name="create" id="create" value="create">
</td>
</tr>
<?php
}
?>
The data will be displayed like:
categoryId1 categoryName1 <Create Button>
categoryId2 categoryName2 <Create Button>
categoryId3 categoryName3 <Create Button>
and so on like this..
Now, suppose When i click on Create Button of CategoryName2, i want to get the categoryId and categoryName of only CategoryName2..
Am using this code:
if(isset($_POST['create']) && $_POST['create']=="create")
{
$cat_id[]=$_POST['categoryId'];
$cat_name[]=$_POST['categoryName'];
}
But with this, am getting all the categoryId and categoryName into the array.. how to get only the selected textbox value..
I want to do it using only PHP and not with JavaScript / JQuery... Am a bit new to PHP... Can someone help me / give me a suggestion about how to do it...
Thanks in advance.
Wrap each one in its own form:
<td>
<form method="POST" action="somePage.php">
<input type="text" name="categoryId" value="<?php echo $value['CategoryId']?>">
<input type="text" name="categoryName" value="<?php echo $value['CategoryName']?>">
<input type="submit" name="create" value="create">
</form>
</td>
Since the content being posted in this case is just these two values, then that's the entire scope of a single form post. So each one is logically/semantically its own form. (There's no rule that says a page can have only one form. Unless you're ever using ASP.NET WebForms, which is a lie.)
Note also that I removed your id attributes. Multiple elements can't have the same id, that's invalid HTML and any behavior as a result becomes undefined.

How do I make my radio buttons checked

I have a table, that generates rows dynamically. I have used radio buttons for each row. I want the radio button to be checked even after I click my submit button.
This is body of my table:
<form action="" method="POST">
<table>
<thead>
<tr>
<td></td>
<td><h3>Date<h3></td>
<td><h3>Status<h3></td>
</tr>
</thead>
<tbody>
<?php for($i=$start;$i<$end;$i++)
{
$add=$ARRAY[$i]['source_address'];
$Status=$ARRAY[$i]['Status'];
$total=$add.$Status;
?>
<tr>
<td><input type="radio" name="ID[]" value="<?php echo $total; ?>"/></td>
<?php
echo'<td>'.$ARRAY[$i]['escl_date'].'</td>';
echo'<td>'.$ARRAY[$i]['escl_status'].'</td>';
?>
</tr>
<?php }?>
</tbody>
</table>
<input type="submit" name="details" value="details" />
How Can I make the radio button selected even after clicking on submit button? Please help me.
You can try this, You need a condition to check with reference value and add checked attribute.
Here your reference value is your POST of ID value. Add this into your radio tag <?php echo $_POST['ID'][0]==$total ? 'checked':'';?>
<input type="radio" name="ID[]" value="<?php echo $total; ?>" <?php echo $_POST['ID'][0]==$total ? 'checked':'';?> />
The correct HTML attribute you are looking for is checked.
Note to other answer: readonly is not necessary, nor is it what OP is asking for.
Since you're doing it in PHP, let's say you're passing something with an HTML markup name of foobar, as follows:
<form><input type="radio" name="foobar"></form>
To make sure that the value is retained even after it is submitted, you could add a code like this (get or post depending on your purpose):
<form><input type="radio" name="foobar" <?php if(isset($_GET[foobar])){ echo "checked"; ?>></form>
This is to make sure that it would be checked if the name was passed in the form.
Note that this is just an example.
Try this, it works:
<input type="radio" checked>

Javascript, PHP, Saving field value

I am trying to save fields data after submited, Becouse after all the fields are good to go but lets say at the server side the user name is already taken so the form return empty and i dont want that there is the option to do it with PHP like that:
<input value="<?php if(isset($userName)) echo $userName; ?>" />
But the problem is with the radio input, If can some one think about solution about the radio with PHP i will be very thankful, Also i was thinking about Javascript so i will have cleaned code and i was thinking about taking the values from the URL but i am using POST for security reasons.
Summary: If anyone have a solution with PHP or Javascript i will be very thankful, Thank you all and have a nice day.
Try this
<form name="myform" action="" method="post">
<input type="radio" name="language" value="Java" <?php echo(#$_POST['language'] == 'Java'?"checked":""); ?> /> Java
<input type="radio" name="language" value="VB.Net" <?php echo(#$_POST['language'] == 'VB.Net'?"checked":""); ?> /> VB.Net
<input type="radio" name="language" value="PHP" <?php echo(#$_POST['language'] == 'PHP'?"checked":""); ?> /> PHP
<input type="submit" />
I think this may help you.
<input type="radio" value="choice1" name="radio_name" <?php echo(#$_POST['radio_name'] == 'on'?"checked":""); ?> />
If you want to automatically select a radio input you can add the attribute checked to it. What you are going to need will look like this :
<form method="POST">
<?php
// You have some short of list of possible value //
$arrRadioValues = array("value1", "value2", "value3");
// You display them //
for ($i=0; $i<count($arrRadioValues); $i++) {
?>
<input
type="radio"
name="radioInputName"
value="<?php echo $arrRadioValues[$i]; ?>"
<!-- If the value that was posted is the current one we have to add the "checked" so that it gets selected -->
<?php if (isset($_POST['radioInputName']) && $_POST['radioInputName'] == $arrRadioValues[$i]) { echo " checked"; } ?> />
<?php
}
?>
<input type="submit" />
</form>
Adding the checked attribute works a little bit in the same as setting a value to an input. It's just that instead of defining the value attributes, you define the checked attribute when you want that radio to be selected.

PHP:confusing about keeping state with hidden filed

I would like to update the choice of poll to update every time that I've submitted.
but It seemed not working. Can anyone advice me about keeping state with hidden field?
and how to clear all the content in current page to display the poll result table?
<?php
if ($_POST['choice']==0)
$a_count = $_POST['a_count']+1;
if ($_POST['choice']==1)
$b_count = $_POST['b_count']+1;
if ($_POST['choice']==2)
$c_count = $_POST['c_count']+1;
if ($_POST['choice']==3)
$d_count = $_POST['d_count']+1;
?>
<html>
<head>
</head>
<body>
<form action="index.php" method="POST">
<table align="center">
<tr><td>Please select</td></tr>
<tr><td><input type="radio" name="choice" value="0">aaaa</td></tr>
<input type="hidden" name="a_count" value="<?php print $a_count ?>">
<tr><td><input type="radio" name="choice" value="1">bbbb</td></tr>
<input type="hidden" name="b_count" value="<?php print $b_count ?>">
<tr><td><input type="radio" name="choice" value="2">cccc</td></tr>
<input type="hidden" name="c_count" value="<?php print $c_count ?>">
<tr><td><input type="radio" name="choice" value="3">dddd</td></tr>
<input type="hidden" name="d_count" value="<?php print $d_count ?>">
<tr><td><input type="submit" value="submit"></td></tr>
</table>
<table align="center" border="1" cellspacing="0">
<tr align="center"><th>Member Name</th><th>Vote</th></tr>
<tr><td>aaaa</td><td><?php echo"$a_count";?></td></tr>
<tr><td>bbbb</td><td><?php echo"$b_count";?></td></tr>
<tr><td>cccc</td><td><?php echo"$c_count";?></td></tr>
<tr><td>dddd</td><td><?php echo"$d_count";?></td></tr>
</table>
</form>
</body>
</html>
The code you submitted looks like it should keep the user's selection/state during their active session (as long as they do not leave that page) - but as soon as they leave it's lost. Also, their "vote" cannot be shared between any other users.
To keep the user's state, explore the use of PHP sessions or storing results in a file/database.
To share the user's vote(s) with other users, explore the use of files or a database.
To show the results table without the form, after the user has "cast their vote", you can do one of two things. You could put an if-statement around the form to check if an answer has already been selected - if so, don't display the form. The other way would be to have the results-table on a separate page that doesn't have the form (just have the form POST to the separate page, or have the page with the form redirect to the separate page).
why not use the database to store values?? And you can put the table that you are using for the input in an if condition so that it does not render when the form is submitted. Hope this helps!

Categories