why if conditon is not behaving properly in comparing values - php

why if conditon is not behaving properly in comparing values one which is coming from database and other coming from form via post..inside while loop of mysql fetch array
<?php
$right=0;
$wrong=0;
$result = mysql_query("SELECT * FROM mcq") ;
$total =mysql_num_rows($result);
echo "total questions are :".$total;
echo "<br>";
while($row = mysql_fetch_array($result))
{
$b=$row['id'];
$a=$_POST['a_'.$b];
$cor=$row['correct'];
if($a==$cor)
$right++;
else
$wrong++;
}
$a is coming from radio buttons from previous page and $cor is coming from database..i m comparing selected value of radio button with cor(correct answer of that value) which is coming from data base.. but condition is not executing rightly so please help me!!!!

The radio button just send one value. Your way to retrieve it is wrong, because you are basing in the ID, but the id should be used in the value.
Usually in HTML you should use some like this:
<input type="radio" name="myradio" value="item1" />
<input type="radio" name="myradio" value="item2" />
<input type="radio" name="myradio" value="item3" checked />
So, you should try to get the value from post:
$myvar = $_POST["myradio"];
Can you paste the radio button code?

Related

Checkbox Insert / Delete From Database

A little about my code:
I have code that dynamically displays emails with a checkbox next to each email.
<?
foreach($er as $row){ ?>
<input name="emailcheckbox" id="emailcheckbox" type="checkbox" value="check" checked="checked">
<?
echo $row[email]."<br><br>";
echo "<input name='emailID' id='emailID' type='hidden' value='".$row[emailID]."' />";
} $emailquery->execute(); ?>
I can't seem to come up with a way that deletes the emailID of each email from a specific database table when you uncheck the checkbox. When you re-check the checkbox, I want to insert it back into the database table.
The emails won't go away, because they are stored in a completely different table than the one I want to insert/remove it from.
I know this is kind of a full question, so I will answer any questions you may have. Thank you in advance for all your help!
first, change your input to
<input name="emailcheckbox[]" value="<?php echo htmlspecialchars($row[email]);?>" ...
so, after you post this form back to server you will have
$_POST['emailcheckbox'] == array('checkedemail1', 'checkedemail2'...)
so you will need to delete all e-mails from your table and insert emails from this array, with this you will delete unchecked ones and save only checked ones
You can do this way:
<input name="emailcheckbox" id="emailcheckbox" type="checkbox" value="[tablename][email]" checked="checked">
Insert Page:
<?
foreach($_POST['emailcheckbox'] as $item)
{
$query = "INSERT INTO ".$item[0]." VALUES(".$item[1].")";
.....
}....
?>

PHP MYSQL checkbox checked

With this line of code, I add every selected checkbox's value into the database:
// ADD ALL TYPES TO PRODUCTIONLOG_TYPE TABLE
$x=1;
$values=array();
foreach($_POST['id'] as $x)
{
$AddToListQuery = "
INSERT INTO
productionlog_type
(productionlogid, typeid)
VALUES
('" . mysql_real_escape_string($_GET['productionlog']) . "', '". mysql_real_escape_string($x) ."')
";
mysql_query($AddToListQuery)or die("query fout " . mysql_error() );
}
I do this so I can echo out the checkbox checked if this is alreayd in the database.
The problem now is, when the user unchecks the checkbox and sends the form, it's not passing any value, right? So I can't delete it from the database...means that the checkbox remains checked.
What can I do about this?
If you know which of the checkboxes will be returned, you can filter out the ones you know something about and only the checkboxes that are not checked remain then.
You can also have a look at some processing of your page before you send the request, with JavaScript for example.
Or you can pass another hidden variable in your form that contains all the checkboxes you should receive in your PHP script. For example:
<input type="hidden" value="id_1,id_2,id_3" />
If you then receive this in your PHP script, you can see which of the ids (of the checkboxes) are expected.
EDIT
If you have:
<form method="POST">
<input type="hidden" name="checks" value="id1,id2" />
<input type="checkbox" name="id1" checked="checked" />
<input type="checkbox" name="id2" />
</form>
And then in PHP:
$arr = split(",", $_POST["checks"]);
foreach ($arr as $x){
if(isset($_POST[$x])){
// Add or update
}else{
// Remove
}
}
Note: I haven't tested the code, but it you just to get an idea on how it works

How to insert multiple textbox values in databse those are checked with checkbox with php?

Below is an explanation of exactly what I want
On the submission form I have multiple checkboxs. when users click on checkbox a textbox appears next to it, where user input text. I want to insert the value of texbox in array and insert it in database with PHP-MYSQL.
for example checkbox1 is index 0=>valueoftextbox, checkbox2 is index 1=>value of texbox2
How can I achieve this?
Serialize received $_POST.
<input type="checkbox" name="smth[]" value="One" />
<input type="checkbox" name="smth[]" value="Two" />
<input type="checkbox" name="smth[]" value="Three" />
<?php
if (isset($_POST)) {
$smth = serialize($_POST['smth']);
$query = mysql_query("INSERT INTO table VALUES ('$smth')") or die(mysql_error());
}
A few pointer to get you going:
1) From PHP use:
echo "<pre>";
print_r($_POST);
echo "</pre>";
That way you can see WHAT is posted from your client-form.
2) Note that an unchecked checkbox ISN'T posted at all.
So if you have following checkbox in your form named "wantsnewsletter".
Then check that from PHP like this:
if (isset($_POST["wantsnewsletter"])){
// It was checked.
} else {
// It wasn't checked
}
Futhermore you just have to think up some sensible naming and do the inserts into you DB.

PHP checkbox value [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Submit an HTML form with empty checkboxes
I'm try to submit some data to the database. I'm trying to submit the value for a checkbox in the form to the database as follow:
<input type='checkbox' name='continue' <?php if ($_POST[continue] == TRUE) echo "value='y' checked='checked'"; else echo "value='n'"; ?> />
when I submit the data when the checkbox is checked, the value will be 'y'. but when the checkbox is checked, there is no data sent to the database.
Please advise.
Solution 1: (the better way)
<input type="checkbox" name="continue"<?php echo (isset($_POST['continue']) ? ' checked="checked"' : ''); ?> />
In your database (?) file:
$continue = isset($_POST['continue']) ? 'y' : 'n';
.
Solution 2: (not recommended)
<input type="hidden" name="continue" value="<?php echo isset($_POST['continue']) ? 'y' : 'n'; ?>" />
<input type="checkbox" name="continue_x"<?php echo (isset($_POST['continue']) ? ' checked="checked"' : ''); ?> onclick="document.getElementsByName('continue')[0].value=this.checked?'y':'n';" />
First of all, when you are trying to validate the checkbox using PHP is not going to do the live check unlike JavaScript that you can do it right away without the need to interact with the server side.
So what were you trying to do I assume that you want to insert a data from the form to the database.
<input type='checkbox' name='continue' <?php if (isset($_POST[continue])) echo
"checked='checked'"; ?> />
So what you are doing with the above code is to check using isset function if the checkbox is checked and you recall the form again the checkbox will still preserve the state and it will still be checked.
Now come to the part where you need to get the value to insert into your database.
if (isset($_POST[continue]))
$cont = 'y';
else $s = 's';
You will need this before you are going to insert the value to the query and instead of doing $_POST[continue] now just use $cont as a value in the query and you will get the correct value.
Try this
<input type='checkbox' name='continue' <?php if ($_POST['continue']==TRUE) { echo "value='y' checked='checked'"; } else { echo "value='n'"; } ?> />
Checked checkboxes are successful (and submit their values), unchecked checkboxes are not (and don't).
You need to give the checkbox a fixed value and then determine if it was checked or not based on the presence (or absence) of the data, not the specific value of data.
The value only matters when you have multiple checkboxes with the same name (e.g. for "Tick every mode of transport you use on a regular basis" type questions.)

radio button value in php

I'm trying to make a simple survey in php
I have a set of radio buttons on a page called sja.php that sends its to sjamail.php page
the problem is that when I go to get
$answer = $_POST['ans'];
I can't seen to do anything like
echo "$answer";
but if I were to throw some logic at it
like
if ($answer == "ans1") {
echo 'Correct';
}
else {
echo 'Incorrect';
}
It will display correct or incorrect (edit: The if/else works correctly and will display the correct answer )
so why is it I can't access the value of the radio button "ans" as a string?
http://www.markonsolutions.com/sja.php
print_r($_POST); will return Array ( [ans] => )
Perhaps the value is something other than text.
Try
var_dump($answer);
or
print_r($answer, TRUE);
Your page works correctly if you select any of the first 4 radio buttons (ans1/2/3/4). But the rest of the radio buttons next to all those images have blank values, which would explain why your posted value is empty if you selected any of those to test with.
You need to make sure that the field in HTML has...
<input type="radio" name="ans" value="ans1" />
<input type="radio" name="ans" value="ans2" />
Also make sure your form method is POST
I had a similar problem with the following:
<input name="03 - Gender" type="radio" value="Masculino"/>Male<br/>
<input name="03 - Gender" type="radio" value="Femenino" required="required"/>Female <br/>
<input type="hidden" name="03 - Gender" value=""/>
but when I removed the third input line (the hidden one) the problem desapeared.
Try this:
$answer = (string)$_POST["ans"];
echo $answer;
You must convert $_POST["ans"] to string.

Categories