Form expires and shows the result after the time is up - php

I am a beginner in PHP. So I am working on a small task in which I want to make a form in which there are MCQS and there is a countdown timer above it of 10 minutes. When the count down timer is up the form should expire and should show the result automatically.
I have made the form as coded below but I don't know what to do so that I can add timer in this code so that the form expires and shows the result of the right selected answers.
<html>
<head>
<title>PHP Quiz</title>
</head>
<body>
<div id="page-wrap">
<h1>Final Quiz for Lip building</h1>
<form action="quiz.php" method="post" id="quiz">
<ol>
<li>
<h3>CSS Stands for...</h3>
<div>
<input type="radio" name="question-1-answers" id="question-1-answers-A" value="A" />
<label for="question-1-answers-A">A) Computer Styled Sections </label>
</div>
<div>
<input type="radio" name="question-1-answers" id="question-1-answers-B" value="B" />
<label for="question-1-answers-B">B) Cascading Style Sheets</label>
</div>
<div>
<input type="radio" name="question-1-answers" id="question-1-answers-C" value="C" />
<label for="question-1-answers-C">C) Crazy Solid Shapes</label>
</div>
<div>
<input type="radio" name="question-1-answers" id="question-1-answers-D" value="D" />
<label for="question-1-answers-D">D) None of the above</label>
</div>
</li>
<li>
<h3>Internet Explorer 6 was released in...</h3>
<div>
<input type="radio" name="question-2-answers" id="question-2-answers-A" value="A" />
<label for="question-2-answers-A">A) 2001</label>
</div>
<div>
<input type="radio" name="question-2-answers" id="question-2-answers-B" value="B" />
<label for="question-2-answers-B">B) 1998</label>
</div>
<div>
<input type="radio" name="question-2-answers" id="question-2-answers-C" value="C" />
<label for="question-2-answers-C">C) 2006</label>
</div>
<div>
<input type="radio" name="question-2-answers" id="question-2-answers-D" value="D" />
<label for="question-2-answers-D">D) 2003</label>
</div>
</li>
<li>
<h3>SEO Stand for...</h3>
<div>
<input type="radio" name="question-3-answers" id="question-3-answers-A" value="A" />
<label for="question-3-answers-A">A) Secret Enterprise Organizations</label>
</div>
<div>
<input type="radio" name="question-3-answers" id="question-3-answers-B" value="B" />
<label for="question-3-answers-B">B) Special Endowment Opportunity</label>
</div>
<div>
<input type="radio" name="question-3-answers" id="question-3-answers-C" value="C" />
<label for="question-3-answers-C">C) Search Engine Optimization</label>
</div>
<div>
<input type="radio" name="question-3-answers" id="question-3-answers-D" value="D" />
<label for="question-3-answers-D">D) Seals End Olives</label>
</div>
</li>
</ol>
<input type="submit" value="Submit Quiz" />
</form>
</div>
</body>
</html>

You need JavaScript for such behavior, let's say you use the following JS:
window.setTimeout(function() {
document.forms['form_name'].submit();
}, 2000);
form_name should be the name of your form. i.e
<form name = "form_name" ... >
</form>
This will delay the post in milliseconds (2 seconds) and then it should take you to another page where you can show the correct answers.
Fiddle

Related

Setting a Radio button from a text document using PHP and HTML

So you may remember me from last week (college student) i'm now trying to get my radio buttons to remember what was entered when the page is refreshed or navigated back to. So i'm using text documents to save the answers which works a dream with everything on my site so far, however i'm having an issue getting them to send the data back to the radio buttons. Anyone know how to do this?
<?php
ob_clean();session_start();
if (isset($_GET['logout'])){
session_destroy();
}
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] == false) {
header("Location: index.php");
}
//Send Data
$tb1 = $_POST['radio1']."\r\n";
$tb2 = $_POST['radio2']."\r\n";
$tb3 = $_POST['rasio3']."\r\n";
$tb4 = $_POST['radio4']."\r\n";
$tb5 = $_POST['rasio5']."\r\n";
if (isset($_POST['submit'])){
$myFile=fopen("DATA/GIP1_Radio1.txt","w") or exit("Can’t open file!");
fwrite($myFile, $tb1);
fclose($myFile);
$myFile=fopen("DATA/GIP1_Radio2.txt","w") or exit("Can’t open file!");
fwrite($myFile, $tb2);
fclose($myFile);
$myFile=fopen("DATA/GIP1_Radio3.txt","w") or exit("Can’t open file!");
fwrite($myFile, $tb3);
fclose($myFile);
$myFile=fopen("DATA/GIP1_Radio4.txt","w") or exit("Can’t open file!");
fwrite($myFile, $tb4);
fclose($myFile);
$myFile=fopen("DATA/GIP1_Radio5.txt","w") or exit("Can’t open file!");
fwrite($myFile, $tb5);
fclose($myFile);
header( 'Location: http://www.murphy.sulmaxmarketing.com/GeneralImpressionPt2.php' ) ;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>General Impression Part 1</title>
<link href="CSS/boilerplate.css" rel="stylesheet" type="text/css">
<link href="CSS/master.css" rel="stylesheet" type="text/css">
<script src="JAVASCRIPT/respond.min.js"></script>
</head>
<body link="black">
<div class="gridContainer clearfix">
<div id="borderDiv">
<div id="navDiv">
<div id="backNavDiv">
<font color="#0099FF"><p align="left"><b> < Feedback</b></p></font>
</div>
<div id="logoutDiv">
<font color="#0099FF"><p align="right"><b>Logout > </b></p></font>
</div>
</div>
<div id="headerDiv">
<p>General Impression Part 1</p>
</div>
<div id="subHeaderDiv">
<p>CULTURE</p>
</div>
<form action="GeneralImpressionPt1.php" method="post" enctype="multipart/form-data" name"FileForm" id="FileForm">
<label for="FormField"></label>
<p>Is the CDP obvious - You Said / We Did Boards; Feedback Stations; Posters?</p>
<div id="radioBtnDiv">
<input type="radio" name="radio1" value="1"> 1<br>
</div>
<div id="radioBtnDiv">
<input type="radio" name="radio1" value="2"> 2<br>
</div>
<div id="radioBtnDiv">
<input type="radio" name="radio1" value="3"> 3<br>
</div>
<div id="radioBtnDiv">
<input type="radio" name="radio1" value="4"> 4<br>
</div>
<div id="radioBtnDiv">
<input type="radio" name="radio1" value="5"> 5<br>
</div>
<p>Are there any Speak Up Coaches on this site?</p>
<div id="radioBtnDiv">
<input type="radio" name="radio2" value="1"> 1<br>
</div>
<div id="radioBtnDiv">
<input type="radio" name="radio2" value="2" > 2<br>
</div>
<div id="radioBtnDiv">
<input type="radio" name="radio2" value="3"> 3<br>
</div>
<div id="radioBtnDiv">
<input type="radio" name="radio2" value="4"> 4<br>
</div>
<div id="radioBtnDiv">
<input type="radio" name="radio2" value="5"> 5<br>
</div>
<p>Are Focus Leader meetings happening?</p>
<div id="radioBtnDiv">
<input type="radio" name="radio3" value="1"> 1<br>
</div>
<div id="radioBtnDiv">
<input type="radio" name="radio3" value="2"> 2<br>
</div>
<div id="radioBtnDiv">
<input type="radio" name="radio3" value="3"> 3<br>
</div>
<div id="radioBtnDiv">
<input type="radio" name="radio3" value="4"> 4<br>
</div>
<div id="radioBtnDiv">
<input type="radio" name="radio3" value="5"> 5<br>
</div>
<p>Mention the Mental Tools – do people understand them?</p>
<div id="radioBtnDiv">
<input type="radio" name="radio4" value="1"> 1<br>
</div>
<div id="radioBtnDiv">
<input type="radio" name="radio4" value="2"> 2<br>
</div>
<div id="radioBtnDiv">
<input type="radio" name="radio4" value="3" > 3<br>
</div>
<div id="radioBtnDiv">
<input type="radio" name="radio4" value="4" > 4<br>
</div>
<div id="radioBtnDiv">
<input type="radio" name="radio4" value="5" > 5<br>
</div>
<p>Ask what ‘Never Harm’ means to the teams.</p>
<div id="radioBtnDiv">
<input type="radio" name="radio5" value="1"> 1<br>
</div>
<div id="radioBtnDiv">
<input type="radio" name="radio5" value="2"> 2<br>
</div>
<div id="radioBtnDiv">
<input type="radio" name="radio5" value="3"> 3<br>
</div>
<div id="radioBtnDiv">
<input type="radio" name="radio5" value="4"> 4<br>
</div>
<div id="radioBtnDiv">
<input type="radio" name="radio5" value="5"> 5<br>
</div>
<div id="loginBtnDiv">
<input id="submit" name="submit" type="submit" value="Next">
</div>
</form>
</div>
</div>
</body>
</html>
You should retrieve the information on documents and then try to check each radio button if it corresponds to input checked. For example:
if (isset($_POST['submit'])){
...
}else{
$filename = "DATA/GIP1_Radio1.txt";
$handle1 = fopen($filename,"r") or exit("Can’t open file!");
$radio1 = fread($handle, filesize($filename));
fclose($handle);
//The same for all files...
}
And then, in the html part, check if the input is the same as the stored in file:
<input type="radio" name="radio1" value="1" <?php if($radio1 == 1){ echo "checked"; ?>>
<input type="radio" name="radio1" value="2" <?php if($radio1 == 2){ echo "checked"; ?>>
...

filter price by radio button in php mysql

i am working on e-commerce website and in products section i want to give a function to sort product by price range like 100 to 500 but i have no idea to connect html radio button with mysql that on click product get sort on screen.
[enter image description here][1]
This is html code for radio button
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
<ul class="radio">
<li><input type="radio" name="radio" id="r1" value="radio"/>
<label for="radio1">100-500</label></li>
<li><input type="radio" name="radio" id="r2" value="radio2"/>
<label for="radio2">600-1000</label></li>
<li><input type="radio" name="radio" id="r3" value="radio"/>
<label for="radio3">1100-1500</label></li>
<li><input type="radio" name="radio" id="r4" value="radio4"/>
<label for="radio4">1600-2000</label></li>
<li><input type="radio" name="radio" id="r5" value="radio5"/>
<label for="radio5">2100-2500</label></li>
</ul>
</form>
And this php and mysql code
<?php
$results1 = mysqli_query($db,"SELECT product_code, product_name, product_img_name, price FROM products where price> 500 ORDER BY id ASC");
if($results1){
$products_item = '<ul class="products">';
while($obj = $results1->fetch_object())
{
$products_item .= <<<EOT
<li class="product">
<form method="post" action="cart_update.php">
<div class="product-content"><h3>{$obj->product_name}</h3>
<div class="product-thumb"><img height="250px" src="../../img/{$obj->product_img_name}"></div>
<div class="product-info">
Price {$currency}{$obj->price}
<fieldset>
<label>
<span>size</span>
<select name="product_color">
<option value="Black">Medium</option>
<option value="Silver">Small</option>
</select>
</label>
<label>
<span>Quantity</span>
<input type="text" size="2" maxlength="2" name="product_qty" value="1" />
</label>
</fieldset>
<input type="hidden" name="product_code" value="{$obj->product_code}" />
<input type="hidden" name="type" value="add" />
<input type="hidden" name="return_url" value="{$current_url}" />
<div align="center"><button type="submit" class="add_to_cart">Add</button></div>
</div></div>
</form>
</li>
EOT;
}
}
?>

Html Checkboxes Are Not Giving Values To PHP

I am creating an html form that contains multiple checkboxes...
Here is my code
Form
<form enctype="multipart/form-data" action="processstart.php" method="post" class="wpcf7-form">
<div class="form-row">
<span class="label--input-group">Services you require</span>
<span class="wpcf7-form-control-wrap services">
<span class="wpcf7-form-control wpcf7-checkbox check-group-input">
<span class="wpcf7-list-item">
<label>
<input type="checkbox" name="services[]" value="Branding" /> <span class="wpcf7-list-item-label">Branding</span>
</label>
</span>
<span class="wpcf7-list-item">
<label>
<input type="checkbox" name="services[]" value="Design" /> <span class="wpcf7-list-item-label">Design</span>
</label>
</span>
<span class="wpcf7-list-item">
<label>
<input type="checkbox" name="services[]" value="Other" /> <span class="wpcf7-list-item-label">Other</span>
</label>
</span>
<span class="wpcf7-list-item">
<label>
<input type="checkbox" name="services[]" value="Development" /> <span class="wpcf7-list-item-label">Development</span>
</label>
</span>
<span class="wpcf7-list-item">
<label>
<input type="checkbox" name="services[]" value="Illustration" /> <span class="wpcf7-list-item-label">Illustration</span>
</label>
</span>
</span>
</span>
</div>
</form>
Here is processstart.php
<?php
if(isset($_POST['services'])) {
foreach($_POST['services'] as $services) {
echo $services;
}
}
?>
Now nothing is getting echoed...I have tried var_dump(get_defined_vars()); and every other variable is defined except these checkboxes. Not even showing "null" there. What is going wrong?
You have missed to close <form> tag and add submit button
<input type="submit" name="submit" value="submit">
</form>
try i have action on same file but you can change action values will be appear after any checkbox is checked else you can't get checkbox values
On your form not any close <form> tag and not submit form action like button or submit button
<?php if(isset($_POST['services']))
{
foreach($_POST['services'] as $services)
{
echo $services;
}
}?>
<html>
<body>
<form enctype="multipart/form-data" action="" method="post" class="wpcf7-form">
<div class="form-row"><span class="label--input-group">Services you require</span><span class="wpcf7-form-control-wrap services"><span class="wpcf7-form-control wpcf7-checkbox check-group-input"><span class="wpcf7-list-item">
<label>
<input type="checkbox" name="services[]" value="Branding" />
<span class="wpcf7-list-item-label">Branding</span></label>
</span><span class="wpcf7-list-item">
<label>
<input type="checkbox" name="services[]" value="Design" />
<span class="wpcf7-list-item-label">Design</span></label>
</span><span class="wpcf7-list-item">
<label>
<input type="checkbox" name="services[]" value="Other" />
<span class="wpcf7-list-item-label">Other</span></label>
</span><span class="wpcf7-list-item">
<label>
<input type="checkbox" name="services[]" value="Development" />
<span class="wpcf7-list-item-label">Development</span></label>
</span><span class="wpcf7-list-item">
<label>
<input type="checkbox" name="services[]" value="Illustration" />
<span class="wpcf7-list-item-label">Illustration</span></label>
</span></span></span></div>
<input type="submit" name="sub">
</form>
</body>
</html>
actually you are not submitting your form. You just specified in 'action'. To achieve your code workable, add a submit button. So that, while clicking it will call processstart.php
<input type="submit">

html5 form not reacting to submit

I've got a simple one-page form, the kind I've written dozens of times, but this behavior is totally new to me. At first, it was submitting on page-load, thus redirecting automatically to the next page in the series. I'm still not sure how I got that to stop, but now it's not doing anything at all when you hit "submit". The page simply sits there.
I've tried stripping out the error-checking scripts, the show/hide script, even jquery itself, then taking the form down to just one input. I tried getting rid of the redirect and just having it output a simple line, then vardump, and still nothing. I hit submit, and no matter what I do, the page just sits there. I've never run into behavior like this before, and firebug et al give me no errors or leads.
If anyone has any ideas at all, no matter how crazy, I'm willing to try. I'm at a loss. Thanks in advance!
<?php
session_start();
include('functions.php');
if ($_POST['submit']) {
$company = $_POST['company'];
$taxid = $_POST['taxid'];
header("location:step2.php");
}
include('head.php'); ?>
<form method="post" name="basic" action="step1.php">
<div class="col70">
<label for="company">Organization/Business name <img src="img/req.jpg" alt="required"></label>
<input type="text" name="company" id="company" value="" />
</div>
<div class="col30">
<label for="taxid">Taxpayer ID# (IEN or SS#) <img src="img/req.jpg" alt="required"></label>
<input type="text" name="taxid" id="taxid" value="" />
</div>
<div class="newcol">
<label for="address">Mailing Address <img src="img/req.jpg" alt="required"></label>
<input type="text" name="address" id="address" value="" />
</div>
<div class="col30 newcol">
<label for="city">City <img src="img/req.jpg" alt="required"></label>
<input type="text" name="city" id="city" value="" />
</div>
<div class="col30">
<label for="state">State <img src="img/req.jpg" alt="required"></label>
<select name="state" id="state" tabindex="<?php echo $tabin; $tabin++; ?>">
<option value="0"></option>
<option value="1">Alabama</option>
</select>
</div>
<div class="col25">
<label for="zipcode">Zip Code <img src="img/req.jpg" alt="required"></label>
<input type="text" name="zipcode" id="zipcode" value="" />
</div>
<fieldset><legend>1. What kind of group/company do you have? <img src="img/req.jpg" alt="required"></legend>
<span id="nfpfp" class="InputGroup">
<label><input name="nfpfp" id="nfpfp_1" type="radio" value="1" />For-profit business.</label>
<label><input name="nfpfp" id="nfpfp_2" type="radio" value="2" />Non-profit 501(c)3 organization.</label>
</span>
</fieldset>
<fieldset><legend>2. How will you use the booth space? Select all that apply. <img src="img/req.jpg" alt="required"></legend>
<span id="type" class="InputGroup">
<label><input name="food" id="type_1" type="checkbox" value="1" />Food sales</label>
<label><input name="retail" id="type_2" type="checkbox" value="2" />Retail sales</label>
<label><input name="activity" id="type_3" type="checkbox" value="3" />Activity</label>
<label><input name="display" id="type_4" type="checkbox" value="4" />Display</label>
<label><input name="other" id="type_5" type="checkbox" value="5" />Other</label>
</span>
</fieldset>
<label for="otherdetails" class="newcol offsides">Enter a short description of your use. (Ex: "BBQ sandwiches", "kite kits", "face painting".) <img src="img/req.jpg" alt="required"></label>
<input type="text" name="otherdetails" id="otherdetails" value="" />
<fieldset><legend>3. Select any/all that apply. Additional questions may appear, if further information is required.</legend>
<span id="additional" class="InputGroup">
<label><input name="raffle" id="raffle_1" type="checkbox" class="switchcheck1" value="1" />I'll be selling raffle tickets and/or holding a raffle at the festival.</label>
<div class="newcol offstate1">
<label for="raffledetails">You'll need written permission from the Exchange Club. Please enter details about the raffle. <img src="img/req.jpg" alt="required"></label>
<textarea name="raffledetails" id="raffledetails" tabindex="<?php echo $tabin; $tabin++; ?>"></textarea>
</div>
<label><input name="trailer" type="checkbox" id="trailer_1" value="1">I'll be bringing a trailer.</label>
<label><input name="outlets" type="checkbox" id="outlets_1" class="switchcheck2" value="1" />I'll require electrical outlets.</label>
<div class="newcol offstate2">
<label for="outletsdetails">How many outlets will you require? <img src="img/req.jpg" alt="required"></label>
<input type="text" name="outletsdetails" id="outletsdetails" />
</div>
</span>
</fieldset>
<input type="button" name="submit" class="a_button" value="submit" />
</form>
The element with name="submit" is of type button and not submit, so it renders a plain button that does nothing (with the intention that you bind some JavaScript to it).
Use type="submit" instead.
Try
<input type="submit" name="submit" class="a_button" value="submit" />
for your submit button.

Javascript isn't being called by submit button in HTML

For some reason my javascript won't execute when my submit button is pressed. It's supposed to leave an error message beside two textfields for a user's name and email address if they are empty, but it's not and I can't figure out why. Normally, when the two boxes are filled, the submit would go to a php page which sends an email. Any suggestions or help with fixing my javascript problem will be greatly appreciated.
This is my javascript:
//my javascript function
<script type='text/javascript'>
function validate_form()
{
$('span.error_message').html('');
var success = true;
$("#validate_form input").each(function()
{
if($(this).val()=="")
{
$(this).next().html("* You must complete this field"); // the error message
success = false;
}
});
return success;
}
</script>
and my form in html:
<form action=".....whatever....." method="POST" id="validate_form" onsubmit="return validate_form();">
<ol>
<li>
<span id="question">___ is your name? My name is Marie.</span>
<input type="text" name="q1" id="q1" />
</li>
<li>
<span id="question">___ are you from? I'm from Paris, France.</span>
<input type="text" name="q2" id="q2" />
</li>
<li>
<span id="question">Dave: ___ you like football</span>
<p>
<input type="radio" name="q6" value="1" id="q6_1" />
<label for="q6_1">Are</label>
<input type="radio" name="q6" value="2" id="q6_2" />
<label for="q6_2">Do</label>
<input type="radio" name="q6" value="3" id="q6_3" />
<label for="q6_3">Does</label>
<input type="radio" name="q6" value="4" id="q6_4" />
<label for="q6_4">Is</label>
</p>
</li>
<div id="username">
<p>
Before we begin, please enter your name and email:
<br />
<label for="user_name">Name: </label><input type="text" name="user_name" id="user_name" />
<span class="error_message" style="color:#FF0000"></span>
<br />
<label for="user_email">Email: </label><input type="text" name="user_email" id="user_email" />
<span class="error_message" style="color:#FF0000"></span>
</p>
</div>
<p style="width: 242px; margin: auto;">
<input type="submit" name="submit" value="Submit your answers" id="but" />
</p>
</form>
Edit: Fixed the problem with the javascript being called, but now the submit button isn't working even with the two textfields being filled.
$("#validate_form input") - I don't see validate_form anywhere; you need to add it or just use: $("input")
Firebug for firefox would help you debug these types of jscript issues. You can also use the Error Console in Firefox if you dont want to install firebug.

Categories