Retain value of radio button after validation php - javascript

My issue is once user click submit the form should check for all questions that the radio button is checked.
It should show the err message on the questions where the user didn't answer.
But right now the form refreshed everytime after the err message is shown and the
previous checked radio button is lost.I also have javascript timer going on,but
since the page get refreshed ,the timer starts all over again.
Im newbie to php,kindly guide me through this issue.Any help is appreciated.
<?php session_start()?>
<?php
$errorexist=false;
if (isset($_POST["submit"])){
for ($y=0;$y<5;$y++)
{
if(empty($_POST["question".$y])){
$errorexist=true;
${"err".$y}="* Please answer question ".($y+1)."<br>";
}
}
if (errorexist==false){
header ("Location: result.php");
exit;}
} ?>
html part of the code
<li>
<span class="error"><?php echo $err0?></span>
<h3>What is the smallest prime number?</h3>
<input type="radio" name="question0" value="A"<?php echo ($question0=='A')? 'checked':''; ?> />2<br>
<input type="radio" name="question0" value="B"<?php echo ($question0=='B')? 'checked':''; ?> />1<br>
<input type="radio" name="question0" value="C"<?php echo ($question0=='C')? 'checked':''; ?> />3<br>
<input type="radio" name="question0" value="D"<?php echo ($question0=='D')? 'checked':''; ?> />4<br>
</li>

Put a condition, where if any radio button misses it displays the
error message and no need to refresh the page. Inside the PHP code,
Request submit button
Request all the radio button. i.e $question1 = $_REQUEST['question1'];
check if all the values are set and not empty if(isset($question1) && !empty($question1) && $question1 !== '')
do this for all, close this condition with the error message.
This might help.

Related

how to Keep check box value after refresh the page for arrays like name="check[0]"

I have 2 check boxes and I want to keep the check box value after page refresh too. I mean if I enable a check box and after I submit the form if I navigate back or refresh the page it should be enable only.
I tried this but not working.
<input type="checkbox" id="enable1" name="check[0]" value= "1"
<?php if (in_array("0", $_POST['check'])) echo "checked='checked'"; ?> />
<input type="checkbox" id="enable2" name="check[1]" value= "1"
<?php if (in_array("1", $_POST['check'])) echo "checked='checked'"; ?> />
Please help me with this how to keep the state of check boxes after page reload
thanks..

Disable submit button after submit and page refresh

What i want is pretty simple, i have a button that submits a value to my database once clicked, what i need is to diable the button parmanently after value has been submited, i have searched and the codes i have seen all enable the button when the page refreshes.. please can someone help me out?
My form and button code as follows:
<form action="<?php echo $editFormAction; ?>" method="POST" name="Status2" id="Status2" onSubmit="return confirm('Are you sure you want to submit?');">
<input name="Confirm2" type="checkbox" id="Confirm2" value="Confirmed" checked="CHECKED" style="display:none;">
<label for="Confirm2"></label>
<input name="UpdateButton3" type="submit" class="art-button" id="UpdateButton3" value="Confirm"/>
<input name="UserIDHiddenField4" type="hidden" id="UserIDHiddenField4" value="<?php echo $row_User['UserID']; ?>">
<input name="Purge2" type="checkbox" id="Purge2" value="You Will Be Rematched Soon!" checked="CHECKED" style="display:none;">
<label for="Purge2"></label>
<input type="hidden" name="MM_update" value="Status2">
</form>
First of all if you submit it, this has nothing to do with javascript. Save this in your database as #georoot says. Then when you submit, save a value in the database. If that value is set, you can disable the button by just HTML:
<input type="submit" <?php if($valueFromDatabase==1){ ?> disabled <?php } ?> >
Best solution could be to set a cookie on client system and/or get user IP and save it into your database, but you should keep in mind, non of them are permanent ! The IP can be changed and the cookie can be removed ! But you can use both cookie and IP check.
Then for each page load, just check if cookie exist (compare the value with ur database value) and/or client IP is already exist in database.
Or if you know who is on the page (if it's a logged user) just add a field in your database and set value = 1 or = 0 and check the field each time it's loaded for same field and for same user.
I wouldn't recommend to use IP check without cookies ! Because it can change frequency !

exam portal questions and answers cache procedure

I am a learner and trying to develop a webportal for online mocktests.
in this portal, after being redirected to the mocktest page, the exam page look something like... a php page having a
1. timer on top
2. two iframes - one on left and one on right - src is php file
The left iframe would contain all buttons displaying questions number.
Right frame would display the question and answer options as radio buttons.
The problem is with caching the questions. Once I check a radio button of questionx, navigate to some other question and come back to the questionx again, the checked option is not visible.. which is to say that the question is again queried from database and displayed afresh. I want already visited questions, selected answers to be cached.
How should I accomplish this?
the mocktest web page looks like this
code for mocktest web page include the frames like below -
<!-- questions numbers left div -->
<div class="left">
<Iframe id="frame1" name="iframe1" src="Questions/QuantitativeAptitude.php" ></Iframe>
</div> <!--div classs=left -->
<!-- display question right div-->
<div class="right">
<Iframe id="frame2" name="iframe2" src="Questions/displayQuestion.php"></Iframe>
</div> <!--div classs=right -->
QuantitativeAptitude.php contains a form, from which a buttom is created
<form name="frm2" >
<input id="questionButton" type="button" name="questionNum" value="<?php echo $questionNum;?>" onclick="parent.hello(this.form.questionNum.value);">
</form>
javascript function defined in the parent page i.e., mocktest page pass the clicked/selected question number to displayQuestion.php file
in displayQuestion.php file, question is retrieved using _GET, query database, display question and answer options through a form.
<FORM method="POST" >
<INPUT TYPE="RADIO" VALUE="1" NAME="theAnswer" onclick="return displayAnswer(value)">
<span class="answerOptions">1. <?php echo $Option1; ?><BR><BR> </span>
<INPUT TYPE="RADIO" VALUE="2" NAME="theAnswer" onclick="return displayAnswer(value)">
<span class="answerOptions">2. <?php echo $Option2; ?><BR><BR></span>
<INPUT TYPE="RADIO" VALUE="3" NAME="theAnswer" onclick="return displayAnswer(value)">
<span class="answerOptions">3. <?php echo $Option3; ?><BR><BR></span>
<INPUT TYPE="RADIO" VALUE="4" NAME="theAnswer" onclick="return displayAnswer(value)">
<span class="answerOptions">4. <?php echo $Option4; ?><BR><BR></span>
<INPUT TYPE="RADIO" VALUE="5" NAME="theAnswer" onclick="return displayAnswer(value)">
<span class="answerOptions">5. <?php echo $Option5; ?><BR><BR></span>
</FORM>
to capture the checked radio button in order to store in database I tried two methodologies -
1. javascript
2. jquery
method 1: javascript
function displayAnswer(answer){
var optionSelected = answer;
alert (optionSelected);
return true;
}
With this methodology, I could see the alert message stating the selected option only for few questions, not all. Also, using javascript, it will be tedious to pass this "optionSelected " variable to another php file where the database would be updated with user selected answer.
**NOTE:**I placed a breakpoint in the above javascript code, using developer tool(F12). As obvious, code did not break for those questions where the alert message was not seen.
method 2:jquery
here I have downloaded jquery-1.12.4.min.js into my xampp project js folder and included into the php file. After, writing jquery, few of the questions itself stopped displaying.
<script>
$('input:radio').change(function(){
var value = $("form input[type='radio']:checked").val();
alert("Value of Changed Radio is : " +value);
});
</script>
Using this jquery, only question 1, 2, 3, 7 are displayed out of 70(in 5 tabs) questions in mocktest. I do not understand why only these questions are displayed and why remaining questions are not displayed.
database sample data
From the data shown in attached image, question 4, 5, 6, 8 popup alert message when radio button is checked.

retaining checkbox and POP Up to add more

I have made a form in which a person puts up his details.
After completing the form and submitting it, I want a popup which should ask "Do you want to add more?","Yes or No ?".
If Yes is chosen, the some values filled in the form should be retained and the rest should appear blank.
On NO the page should get freshened with everything blank.
I am able to retain the values of the drop down items and text fields,but I am not able to retain the values of the check box items.
My check box code :
<label>Hotel Facilities</label>
</td><td class="row1"> :</td> <td class="row1" width="70%">
<div style=" font-weight:normal; color: #4B4B4B;font-size: 12px; width:260px; background:#D2D2D2; max-height:300px; overflow:scroll;overflow-x:hidden;">
<?php $dcs=mysql_query("select * from `tblfacility` where `Type`='Rm'");
while($dc=mysql_fetch_array($dcs)){?>
<input type="checkbox" name="check_list[]" value="<?php echo $dc['FId']; ?>">
<?php echo $dc['Facility'];?> <br />
<?php }?>
</div>
I tried to retain by using:
<input name="check_list[]" type="checkbox" value="<?php echo $dc['FId']; ?>" <?php if(isset($_POST['check_list[]'])) echo "checked='checked'"; else echo $dc['Facility']; ?>
but it did not worked
Please help me how to retain its values after submitting that page.
And the pop up I am at present using is just an alert message and hence it just have OK button:
<?php
if(isset($_POST['submit'])){
echo "<script>alert ('Do You want to Add More ?');</script>";
$HId=$_POST['HId'];
?>
hence it is just showing OK button after submitting .
Please guide how to put Yes or No button after submitting hence is Yes some values will be retained and if NO the whole from will be refreshed.
Instead of checked='checked' just use "selected" or "selected=true". I hope it works!

how to bind click event to checkbox label

I'm hoping a javascript / jquery guru can help me out.
I'm trying to figure out a way to launch a popup box (fancybox) from the click of a checkbox label. I don't want to effect the checkbox function which filters data when clicked.
I would welcome any thoughts on how I can make this happen. I've tried an onclick function on the label but that doesn't work.
I want to show a fancybox when you click on a persons name in the checkbox label. Here's the site:- (example: jonathanlyon)
http://monkeygetnews.jonathanlyon.com/bs.php
The checkboxes are built dynamically and I need to create a click event on the label that opens a fancybox. Here's the code to build the checkboxes:-
<input type="checkbox" id = "dn" value="dept_<?php echo $currdept;?>" onchange="doSearch();return false;" />
<label><?php echo $currdept;?></label>
<ul class="dept_<?php echo $currdept;?>">
<li>
<input type="checkbox" name = "un" value="<?php echo $usernames;?>" <?php echo $include;?> onchange="doSearch();return false;" />
<label><?php echo $usernames;?></label>
</li>
<?php
$dept = $currdept;
} else {
?>
<li>
<input type="checkbox" name = "un" value="<?php echo $usernames;?>" <?php echo $include;?> onchange="doSearch();return false;" />
<label><?php echo $usernames;?></label>
</li>
<?php
$dept = $currdept;
}
?>
Thanks
Jonathan
Very simply put you can use jquery like this
$('label').click(function() {
alert($(this).html());
}
again, this is a very simple example and would need improvement
You need to wrap checkbox tag with label tag to make label clickable like
<label><input type="checkbox" name="checkbox" value="value"/>dasfaha</label>
Edit:
Sorry guys. I didn't look carefully. Here is how to detect label or checkbox.
How it works is that when you click unbounded label, checkbox won't be changed. However, when you click checkbox, ':checked' will be changed. Thus, you can put a variable to store previous value of checked, then compare with current checked.
http://jsfiddle.net/Xtyah/

Categories

Resources