Sorry for the dumb question, though I'm a little new to PHP.
I've tried a lot of other ways to do this, but simply couldn't get this to work... Actually I want it to make it that I can have different functions attached to each checkbox that when a user selects a checkbox or another, and clicks the submit button, it triggers the specific functions. But I cannot get the checkboxes to work. It either works with only one selected, or if I check the 1st one then the 4th one, it outputs the 4th's code.
Any other ways of doing this?
Here is my attempt:
1.php
<form method="POST" action="2.php">
<input type="checkbox" name="test[]" value="test1" />test1<br />
<input type="checkbox" name="test[]" value="test2" />test2<br />
<input type="submit" name="submit" value="submit" />
</form>
2.php
$val = $_POST['test'];
if(isset($val)==true){
for($i = 0 ; $i<count($val) ; $i++){
if($val=='test1'){
echo $val;
die();
}elseif($val=='test2'){
echo $val;
die();
}else{
echo "fail";
die();
}
}
}else{
return false;
}
Thank you.
Try:
$vals = $_POST['test'];
$valsCount = count($vals);
if ($valsCount > 0) {
foreach ($vals as $val) {
switch ($val) {
case 'test1':
echo $val;
break;
case 'test2':
echo $val;
break;
default:
echo 'Fail';
break;
}
}
}
As another option, if you're looking to call functions based on the value of the checkbox, you could do something like this ...
I've compressed it all into one file for simplicity, but this is the general idea ...
<form method="post" action="<?php echo $_SREVER['PHP_SELF']; ?>">
<input type="checkbox" name="boxes[]" value="box1">Box 1</input><br />
<input type="checkbox" name="boxes[]" value="box2">Box 2</input><br />
<input type="checkbox" name="boxes[]" value="box3">Box 3</input><br />
<input type="checkbox" name="boxes[]" value="box4">Box 4</input><br />
<input type="checkbox" name="boxes[]" value="box5">Box 5</input><br />
<input type="submit" value="Go!" />
</form>
<?php
class boxProcessor
{
public function box1()
{
echo "<p>You've found box 1.</p>";
}
public function box2()
{
echo "<p>You've found box 2.</p>";
}
public function box3()
{
echo "<p>You've found box 3.</p>";
}
public function box4()
{
echo "<p>You've found box 4.</p>";
}
public function box5()
{
echo "<p>You've found box 5.</p>";
}
}
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$boxes = $_POST['boxes'];
if(empty($boxes)){
echo "<p>Nothing to do ...</p>";
} else {
$proc = new boxProcessor();
foreach($boxes as $box){
$proc->$box();
}
}
}
?>
You're pretty close with your code as is, you just have to take into account array indexes.
for ($i = 0, $length = count($val); $i < $length; $i++)
{
// add [$i] to $val to access an index of $val
if ($val[$i] == 'test1')
Try changing the names from test1[] to test1 in 1.php and also check a typo on line 7- 2.php.
Hope this helps.
There are a couple of issues that probably don't have anything to do with the real problem:
1) You talk about "1st" and "4th" checkboxes ... but your code only shows two checkboxes
2) Your example misspells "tes2" (so PHP won't/can't find it)
3) You should probably get rid of all the "die()" clauses
SUGGESTION:
Check out this link:
http://www.html-form-guide.com/php-form/php-form-checkbox.html
Related
for training purposes i've made a little code which outputs the value of a checkbox. this works well, but since i am able to check multiple checkboxes i want them to add up. this is my code;
<table border=0 cellpadding=0 cellspacing=0 width=100%>
<form name="orderform" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="checkbox" name="korting" value=15 /> Student 15% <br>
<input type="checkbox" name="korting" value=10 /> Senior 10% <br>
<input type="checkbox" name="korting" value=5 /> Klant 5% <br>
To output the value ive written;
<?php
if(isset($_POST["korting"]))
{
if($_POST["korting"]==15)
{
echo ("15 procent korting");
}
else if ($_POST["korting"]==10)
{
echo ("10 procent korting");
}
else if($_POST["korting"]==5)
{
echo ("5 procent korting");
}
else if(isset($_POST["korting"]) && (isset($_POST["korting"])))
{
if($_POST["korting"]==25)
{
echo ("25 procent korting");
}
}
}
?>
As long as one checkbox is checked, everything works fine. as soon as is check more than one it only uses the last one. I've tried multiple thing like:
else if(isset($_POST["korting"]) && (isset($_POST["korting"]))
{
echo ("25 procent korting");
}
and
else if($_POST["korting"=15] && $_POST["korting"]=10)
{
echo ("25 procent korting");
}
both do not give an error on the page but also don't work. I know it's probably better to use completely different approach but for now this is how the book teaches me
Greetings,
Lennart
What you need is an array. The checkboxes would look like this:
<input type="checkbox" name="korting[]" value="15" />
<input type="checkbox" name="korting[]" value="10" />
$_POST['korting'] would then be an array of values, like this:
array(
0 => 15,
1 => 10
)
You can loop through them to print out the values:
foreach ($_POST['korting'] as $korting_value) {
echo $korting_value . "<BR>";
}
If you want to add up the values, you can use array_sum:
echo array_sum($_POST['korting']);
How do I check another checkbox when I toggled an other checkbox?
My code looks like this:
<form method="post"><input type="checkbox" name="checkall" value = "POST" />
</form>
<?php
foreach($records as $r){
if(isset($_POST["checkall"]) == 'POST'){
$chc = "checked = 'checked'";
}
else{
$chc = "";
}
}
?>
<form method="POST">
<input type="checkbox" name="<?php echo escape($r->id); ?>" class="check" value="POST" <?php echo $chc ;?> />
</form>
The code doesn't work can anybody help me please.
Thanks a lot!
Job
...
foreach($records as $r){
if($_POST["checkall"] == 'POST')
{
...
isset() function will tell you $_POST['checkall'] is there or not and also checks it has a value in it or not.
if you want to comapare $_POST['checkall'] with some value, you have to just compare like example above. If you want both, then use two if conditions like below
...
foreach($records as $r)
{
if(isset($_POST['checkall'])
{
if($_POST["checkall"] == 'POST')
{
...
...
}
}
That would help you. First of all, before putting up the codes, plan a structure of your page, what you have to do and what visitors have to do. WIthout plan, you will end up reaching nowhere and stuck. The code you have presented is not recommended.
If you put some more codes in your questions, we could help you to some extent.
I was wondering if you can help me with my php function
I have created a quiz which uses radio buttons and a submit button. I want the function to check if a specific radio button is selected and if so add 1 onto the user score. however nothing is being added the function for this is
function Score()
{
if(isset($_POST['correctAnswer'] ))
{
$answer=$_POST['correctAnswer'];
$_SESSION['score']=$userScore+1;
}
else
{
$_SESSION['score']=$_SESSION['score'];
}
}
and the form in which it is submitted is
echo '<strong>'."$theQuestion".'</strong><br>';
?> <form name="correctAnswer" form method="post" action="quiz.php" onSubmit="Score()">
<?php
echo "$theAnswer1";?> <input type="radio" id="correct_answer" name="correctAnswer">
<?php
echo "<br>$theAnswer2"; ?> <input type="radio" id="wrong_answer1" name="wrongAnswer1">
<?php
echo "<br>$theAnswer3"; ?> <input type="radio" id="wrong_answer2" name="wrongAnswer2">
<?php
echo "<br>$theAnswer4"; ?> <input type="radio" id="wrong_answer3" name="wrongAnswer3">
<input type="hidden" name="score" value="userScore">
<br><input type="submit" value="Submit Answer">
</form>
Hope you can help
You have to pass the counter as an argument of the function like this
function Score($userScore){
//Do the rest
}
Looks like the $_SESSION['score'] is undefined
function Score() {
if (!isset($_SESSION['score'])) {
$_SESSION['score'] = 0;
}
if (isset($_POST['correctAnswer'])) {
$_SESSION['score']++;
}
}
so i have this code fragment here..
if($numTF > 0)
{
echo "TRUE-AND-FALSE QUESTIONS: Enter them below followed by their correct answer.";
echo "<br>";?>
<form method="post" action="" name="quizform">
<?php for ($i=1; $i<=$numTF; $i++)
{
echo "Question"." ".$i;
?>`
<p><textarea name='question<?php echo $i; ?>' rows=3 cols=90></textarea></p>
<input type="radio" name="answer<?php echo $i; ?>" value="True"> True
<input type='radio' name="answer<?php echo $i; ?>" value="False"> False<br><br><br>
<?php
}
}
... i am making a quiz maker in php...
the first thing to do is to set up the desired number of questions, so the value entered will go on the $numTF variable. Depending on the entered value, the textarea part will be printed. and there will be different names for each text area. AND THE CODE ABOVE IS WHERE U PRINT THE FORMS AFTER U ENTER THE DESIRED VALUE.
The next thing is to save that in a database. since the name of each textarea will be based on a variable value($i) that is used in a loop (name="answer") , HOW CAN I USE IT IN $_POST??? Like, would i do it like this?? ($_POST['question']).
HOW CAN I SAVE THESE QUESTIONS IN A DATABASE??
PLEASE HELP ME ....
I WOULD BE SO MUCH MUCH MUCH GRATEFUL FOR A LIL HELP.
<?
var_dump($_POST);
?>
<form method="post">
<?
$numTF=4;
if($numTF > 0)
{
echo "TRUE-AND-FALSE QUESTIONS: Enter them below followed by their correct answer.";
echo "<br>";?>
<form method="post" action="" name="quizform">
<?php for ($i=1; $i<=$numTF; $i++)
{
echo "Question"." ".$i;
?>`
<p><textarea name='question[<?php echo $i; ?>]' rows=3 cols=90></textarea></p>
<input type="radio" name="answer[<?php echo $i; ?>]" value="True"> True
<input type='radio' name="answer[<?php echo $i; ?>]" value="False"> False<br><br><br>
<?php
}
}
?>
<input type="submit" name="submit" value="submit"/>
</form>
Use $_POST['question'][1] // To get first question
Use $_POST['answer'][1] // To get first answer
Use loop to get all question and answers
I agree with Sachin as far as using name='question[]'. To answer question a little more as far as storing it in a database. Personally I would use a JSON array.
$store_answers = json_encode($_POST['answer']);
$store_questions = json_encode($_POST['question']);
Then just store $store_string in a TEXT field in your database. Then when you pull it back out of the database you can simple use:
$answers = json_decode($store_answers);
$questions = json_decode($store_questions);
Then you can loop through using a foreach like so:
foreach($questions as $key=>$question) {
echo "Question $key = {$answers[$key]} <br />";
}
This will display the results for each question.
I am creating a new website. For that website I will need to transfer a quantity and amount value from one if condition statement into another if condition statement. Both if statements are accessed by separate submit buttons named "checkamt" & "buy".
I need to transfer the "quantity", "value", and "net" values to and from the checkamt if statement to the buy if statement.
Here's my code:
<form action="index.php" method="post">
<input type="submit" name="checkamt" value="Check Amount"/>
<input type="submit" name="buy" value="Buy"/>
</form>
<?php
if(isset($_POST[checkamt]))
{
$qun=1;
$val=5000;
$total=$qun*$val;
}
if(isset($_POST[buy]))
{
echo $qun;
echo $val;
echo $total;
}
?>
I think that the problem you're having is that variables don't persist on page change. If you want that, you'll need to use a session. First, you must call session_start before anything, including HTML, is sent to the user. Then, you can use the $_SESSION variable.
<?php
session_start();
?>
<form action="index.php" method="post">
<input type="submit" name="checkamt" value="Check Amount"/>
<input type="submit" name="buy" value="Buy"/>
</form>
<?php
if(isset($_POST[checkamt]))
{
$_SESSION['qun']=1;
$_SESSION['val']=5000;
$_SESSION['total']=$qun*$val;
}
if(isset($_POST[buy]))
{
echo $_SESSION['qun'];
echo $_SESSION['val'];
echo $_SESSION['total'];
}
?>
Improve your English! Not sure if this is what you want, but if you want to share the values of your variables between the two ifs? You have to declare them at a higher scope than your if:
<?php
$qun = 0;
$val = 0;
$total = 0;
if(isset($_POST[checkamt]))
{
$qun=1;
$val=5000;
$total=$qun*$val;
}
if(isset($_POST[buy]))
{
echo $qun;
echo $val;
echo $total;
}
?>
Not sure if I'm getting the question right, but why not do something like this :
if(isset($_POST[checkamt]) || isset($_POST[buy]))
{
$qun=1;
$val=5000;
$total=$qun*$val;
echo $qun;
echo $val;
echo $total;
}
You need to track your variables between the different forms. You can use SESSION like Xeon06 suggested, or do the following. I'm only showing for $qun:
<?php
if(isset($_POST['checkamt'])) {
$qun=1;
}
if(isset($_POST['buy'])) {
echo $qun;
}
?>
<form action="index.php" method="post">
<input type="hidden" name="qun" value="<?php echo $qun; ?>" />
<input type="submit" name="checkamt" value="Check Amount"/>
<input type="submit" name="buy" value="Buy"/>
</form>