Set a specific Time duration for page - php

I would like to specify the duration of the display of a form. Suppose there is a variable with the value x=(10), whenever I call this page, the timer should be based on the current time and the form should be displayed for 10 minutes and after this time the form should be sent automatically.
<?php
include 'db_conn.php';
if (isset($_GET['id'])) {
$var=$_GET['id'];
$query="SELECT * FROM addquiz WHERE quz_id='$var' ";
$db=mysqli_query($conn,$query);
$res=mysqli_fetch_array($db);
$noquestions=$res['noquestion'];
$quiz=$res['title'];
}?>
<div class="container">
<form method="POST" class="form-horizontal" >
<div style="margin-left: 30%;"><b>Your Quiz: <?php echo $quiz;?></b>
<input type="text" name="user_name" placeholder="Enter Your Name" style="height: 30px; width: 30%;"></div>
<div class="row">
<div class="col-md-12">
<div class="panel" style="margin:5%">
<?php
$count=1;
$que="SELECT * FROM addques WHERE quz_id='$var'";
$dbd=mysqli_query($conn,$que);
while ($cmd=mysqli_fetch_array($dbd)) {
$quest=$cmd['qusname'];
$ans_id=$cmd['ans_id'];
$opt1=$cmd['qpta'];
$opt2=$cmd['optb'];
$opt3=$cmd['optc'];
$opt4=$cmd['optd'];
$answ=$cmd['answer'];?>
<b>Question <?php echo $count++;?> :<br><?php echo $quest;?></b><br><br>
<fieldset>
<input type="hidden" name="ansid[]" value="<?php echo $ans_id; ?>">
<input type="checkbox" name="ans[]" value="1"><?php echo $opt1;?><br><br>
<input type="checkbox" name="ans[]" value="2"><?php echo $opt2;?><br><br>
<input type="checkbox" name="ans[]" value="3"><?php echo $opt3;?><br><br>
<input type="checkbox" name="ans[]" value="4"><?php echo $opt4?><br><br><br>
</fieldset>
<?php } ?>
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
</form>
<div class="button" style="margin-left: 60%;"> View Result </div>
</div>
</div>
</div>
<?php
$i=0;
if (isset($_POST['submit'])) {
$name=$_POST['user_name'];
while ( $i<$noquestions ) {
$query="INSERT INTO `result`( `quz_id`, `ans_id`, `answer`,`user_name`) VALUES('";
$query.=$var . "', '";
$query.=$_POST['ansid'][$i] . "', '";
$query.=$_POST['ans'][$i] . "', '";
$query.=$name . "' )";
$db=mysqli_query($conn,$query);
$i++;
}
}
?>

It depends on what you are trying to achieve.
As you say "at each visit of the page" it could be done like this with javascript:
window.onload = function () {
window.setTimeout(function () {
document.form.submit();
}, 5000);
};
Further you could set the timeout from your php variable and like the comment says above, you could validate the taken time on the server side with php to make sure no one is cheating.

Related

displaying php in an echo nested in html

I'm trying to echo a piece of text with PHP nested in it, anyone know how? I've tried it like this:
function update_category(){
global $connection;
if (isset($_GET['edit'])) {
$edit_cat_key = $_GET['edit'];
$query = "SELECT * FROM categories WHERE cat_id = {$edit_cat_key }";
$edit_cat_query = mysqli_query($connection, $query);
echo '<form action="categories.php" method="post">
<div class="form-group">
<label for="new-cat-title">New name of category</label>
<input value="<?php if(isset($cat_title)){echo $cat_title;} ?>" type="text" class="form-control" name="new-cat-title">
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit" name="submit" value="Rename Category">
</div>
</form>';
while($row = mysqli_fetch_assoc($edit_cat_query)) {
$cat_title = $row['cat_title'];
$cat_id = $row['cat_id'];
}
}
}
It prints: <?php if(isset($cat_title)){echo $cat_title;} ?>
I've tried it like this:
<input value="' . <?php if(isset($cat_title)){echo $cat_title;} ?> . '" type="text" class="form-control" name="new-cat-title">
I'd do it like this (showing only the echo part inside the php code):
echo '<form action="categories.php" method="post">
<div class="form-group">
<label for="new-cat-title">New name of category</label><input value="';
if(isset($cat_title)){echo $cat_title;};
echo '" type="text" class="form-control" name="new-cat-title"></div>
<div class="form-group">
<input class="btn btn-primary" type="submit" name="submit" value="Rename Category">
</div>
</form>';
This echoes the two strings (in single quotes) and in between them the variable depending on the condition, keeping the double-quote pairs of the attribute values intact.
Echo is a php statement. You use it to display an output, as a string. What you need is, replacing echo ' with ?> and </form>'; with </form><?php. These closing(?>) and opening(<?php) tags let you decide which part of your document should be processed as PHP. So PHP will ignore the rest of the document.
Based on Johannes' answer I managed to figure out an answer. I need to close the PHP tags and instead of echoing the wished result, put it in the while loop.
Like this:
function update_category(){
global $connection;
if (isset($_GET['edit'])) {
$edit_cat_key = $_GET['edit'];
$query = "SELECT * FROM categories WHERE cat_id = {$edit_cat_key }";
$edit_cat_query = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($edit_cat_query)) {
$cat_title = $row['cat_title'];
$cat_id = $row['cat_id'];
?>
<input value="<?php echo $cat_title; ?>" type="text" class="form-control" name="cat_title">
<?php
}
}
}

How i can get value from radio button form in php?

Question.php
<?php
include 'Pre-function.php'
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="CSS/Start.css">
</head>
<body>
<div class="nav">
Home
News
Contact
</div>
<div class="question">
<div class="A4">
<form action="Answer.php" method="post">
<?php getQuestion($conn); ?>
<input type="submit" name="Submit" value="Submit">
</form>
</div>
</div>
</body>
</html>
Its html page to ask question
Pre-function.php
<?php
include 'conn.php';
function getQuestion($conn) {
$query = "SELECT * FROM question ";
$result = mysqli_query($conn, $query);
if($result){
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$question_id = $row['question_id'];
$question_body = $row['question_body'];
$option_a = $row['option_a'];
$option_b = $row['option_b'];
echo '
<h2 class="qtitle">'.$question_body.'</h2>
<label for='.$question_body.'>Yes</label>
<input type="radio" name="'.$question_id.'" value="Yes">
<input type="hidden" name="option_a" value="'.$option_a.'">
<label for="'.$question_body.'">No</label>
<input type="radio" name="'.$question_id.'" value="No">
<input type="hidden" name="option_b" value="'.$option_b.'">
<input type="hidden" name="submitted" value="submitted"><hr>';
}
}
}
?>
Basically this form asked question whether yes or no using radio button. $option_a == 'Yes' and $option_b == 'No'. The question is like this "Are you have fever ?". So when i submit the value did not pass to the 'Answer.php' page.
'Answer.php' page.
<?php
include 'conn.php';
if(isset($_POST['Submit']) && !empty($_POST['Submit'])){
echo $_POST['option_a'];
echo 'succeed';
}
else{
echo 'no data';
}
?>
In this page have error undefined_index value but still echo succeed.
Your HTML code should look like the following:
<input type="radio" name="whatevername" value="Yes">
<input type="hidden" name="whatevername" value="No">
You can use PHP to insert whatever values you want but you need the radio button name to be the same.
Then if you want to echo that in PHP you'd use:
echo $_POST['whatevername']; //same name you used in the form
You are passing value as name for radio buttons
name="'.$option_a.'"
This will result you in name ="Yes"
And you are trying to fetch echo $_POST['option_a']; where option_a is not defined.
Try this
<input type="radio" name="'.$question_id.'" value="Yes">
<input type="hidden" name="option_a" value="'.$option_a.'">
Same for other radio button
Try this one :
<?php
include 'conn.php';
function getQuestion($conn) {
$query = "SELECT * FROM question ";
$result = mysqli_query($conn, $query);
if($result){
echo '<div class="A4">';
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$question_id = $row['question_id'];
$question_body = $row['question_body'];
$option_a = $row['option_a'];
$option_b = $row['option_b'];
echo '
<div class="A4">
<h2 class="qtitle">'.$question_body.'</h2>
<form action="Answer.php" method="post">
<label for='.$question_body.'>Yes</label>
<input type="radio" name="radioQuestions[]" value="'.$question_id.'-'.$option_a.'">
<label for="'.$question_body.'">No</label>
<input type="radio" name="radioQuestions[]" value="'.$question_id.'-'.$option_b.'">
<input type="hidden" name="submitted" value="submitted"><hr>';
}
echo'
<input type="submit" name="Submit" value="Submit">
</form>
</div>';
}
}
?>
<?php
include 'conn.php';
if(isset($_POST['submitted']) && !empty($_POST['submitted'])){
$questionAndOptions = $_POST['radioQuestions'];
foreach ($questionAndOptions as $questionAndOption) {
$arrQuestionAndOption = explode("-", $questionAndOption);
echo $arrQuestionAndOption[0]; //question
echo $arrQuestionAndOption[1]; //option
}
echo 'succeed';
}
else{
echo 'no data';
}
?>

PHP Dropdown Menu from DB column values

WHAT I STARTED WITH
My php
<?php
mysql_select_db("$db") or die(mysql_error());
$sql = "SELECT kpi FROM pin_kpi_types";
$query = mysql_query($sql);
echo '<select name="KPI" style="width: 400px">';
while ($row = mysql_fetch_assoc($query)) {
echo '<option>'.$row['KPI'].'</option>';
}
echo '</select>';
?>
My current label/input method that requires me to manually type.
What I am trying to do is pull a query from a table to select the only available KPI types.
This is working now.
<p>
<label for="KPIType" id="preinput">Choose KPI Type: </label>
<input type="text" name="kpi_type" required placeholder="Lead" id="inputid" />
</p>
WHERE I WAS AT PREVIOUS
Edit; Unfortunately the answer posted by Lucky didn't quiet help me, but it steered me in the right way.
I am now have duplicate fields? It isn't posting the value into the DB. either.
Here is a screenshot...
http://s30.postimg.org/o1c2l9yr5/Untitled.png
Can someone steer me in right direction of where I went wrong? :)
Thank you in advance for any help.
<?php
include('db.php');
$select=mysql_query("SELECT * FROM pin_kpi_types");
$i=1;
while( $userrow=mysql_fetch_array($select) )
{
$kpi_id =$userrow['KPI_ID'];
$kpi =$userrow['KPI'];
?>
<style>
#CreateStatusTypeFORM label {display: inline-block;width: 10em; text-align: right;padding-right: 0.5em;}
</style>
<div id="CreateStatusTypeFORM">
<form action="insert.php" method="post" name="insertform">
<p>
<div align='center'><label for="StatusColor" id="preinput">Choose A Color: </label>
<ui-colorpicker ng-model="targetColor"></ui-colorpicker></div>
<input type="hidden" name="pinstatus_color" value="{{targetColor}}" id="inputid" />
</p>
<p>
<label for="StatusName" id="preinput">Set Status Name: </label>
<input type="text" name="pinstatus_type" required placeholder="Come Back" id="inputid"/>
</p>
<p>
<label for="StatusName" id="preinput">Available KPI Types: </label>
<select name="<?php echo $kpi; ?>" style="width: 237px">
<option name="KPI" required id="inputid" value="<?php echo $kpi; ?>"><?php echo $kpi; ?></option>
</select>
</p>
<?php } ?>
<input type="submit" name="send" value="Submit" id="inputid1" />
</p>
</form>
WHERE I AM AT NOW
This is where I am at. I now can get the dropdown correct without duping it over and over. However, now I cannot get the value to post the actual selected KPI to the DB. everything else submits data to the db, but KPI_type option.
<style>
.StatusForm {padding-left:75px;}
.CreateStatusTypeFORM {padding:25px;border-style:solid; border-color:solid black; width: 500px;background-color:#C4C4C4;}
#CreateStatusTypeFORM label {display: inline-block;width: 100em;}
#CreateStatusTypeFORM input {border-color:solid black;}
#CreateStatusTypeFORM input[type=text] {padding:5px; border:1px solid #666; -webkit-border-radius: 5px; border-radius: 5px;}
</style>
<div class='StatusForm'>
<div class="CreateStatusTypeFORM">
<H3> ADD New Status </H3>
<form action="insert.php" method="post" name="insertform">
<p>
<label for="StatusColor" id="preinput">Choose A Color: </label>
<ui-colorpicker ng-model="targetColor"></ui-colorpicker>
<input type="hidden" name="pinstatus_color" value="{{targetColor}}" id="inputid" required placeholder=""/>
</p>
<p>
<label for="StatusName" id="preinput">Set Status Name: </label>
<input type="text" name="pinstatus_type" required placeholder="" id="inputid"/>
</p>
<p>
<label for="KPI" id="preinput">Available KPI Types: </label>
<select>
<?php
include('db.php');
$select2=mysql_query("SELECT * FROM pin_kpi_types ORDER BY KPI_ID DESC");
while( $userrow=mysql_fetch_array($select2) )
{
$kpi =$userrow['KPI'];
$kpi_id =$userrow['KPI_ID'];
?>
<option name="kpi_type" required id="inputid" value="<?php echo $kpi; ?>"><?php echo $kpi; ?></option><?php } ?>
</select>
</p>
<input type="submit" name="send" value="Submit" id="inputid1" />
</p>
</form>
</div>
</div>
This is the insert function.
<?php
ob_start();
include("db.php");
if(isset($_POST['send'])!="")
{
$pinstatus_type =mysql_real_escape_string($_POST['pinstatus_type']);
$kpi_type =mysql_real_escape_string($_POST['kpi_type']);
$pinstatus_color =mysql_real_escape_string($_POST['pinstatus_color']);
$update =mysql_query("
INSERT INTO pin_status_types(
pinstatus_type,
kpi_type,
pinstatus_color,
created
)
VALUES(
'$pinstatus_type',
'$kpi_type',
'$pinstatus_color',
now()
)
");
if($update)
{
$msg="Successfully Updated!!";
echo "<script type='text/javascript'>alert('$msg');</script>";
header('Location:index.php');
}
else
{
$errormsg="Something went wrong, Try again";
echo "<script type='text/javascript'>alert('$errormsg');</script>";
}
}
ob_end_flush();
?>
mysql_select_db("$db") or die(mysql_error());
$sql = "SELECT kpi FROM pin_kpi_types";
$query = mysql_query($sql);
echo '<label for="KPIType" id="preinput">Choose KPI Type: </label>';
echo '<select name="KPI" style="width: 400px" required id="inputid">';
echo '<option value=''>Lead</option>';
while ($row = mysql_fetch_assoc($query)) {
echo '<option value='.$row['KPI'].'>'.$row['KPI'].'</option>';
}
echo '</select>';
<?php
include 'config.php';
$sql_states=mysqli_query($conn,"SELECT * FROM master_states");
?>
<section Name="state_name">
<option value="">section1</option>
<option value="">section2</option>
</section>

POST and GET at the same time

hello everyone sorry but am just beginner in php , mysql ... i was developing question and answer website which i have page for all Question (Q.php) and page for displaying a specific question (QR.php) and get information according to data sent from Q.php via url ($_GET['start'] i also have page to confirm that the answer is already submitted .... but i got error when entering the id from get method and the message from post method ... any answer will be appreciated
Q.php
<?php
include("pagination.php");
if(isset($res))
{
while($result = mysql_fetch_assoc($res))
{
echo '<div class="shop-item">' ;
echo ' <div class="price">' ;
echo $result['Inquirer'] ;
echo ' </div>' ;
echo ' <div class="price">' ;
echo $result['question'] ;
echo ' </div>' ;
echo ' <div class="actions"> ';
echo '<input type="button" class="btn btn-large " value="More Info" onclick="window.location=\'QR.php?start=' . urlencode($result['id']) . ' \';" />';
echo '</div> ';
echo ' </div> ';
}
}
?>
QR.php
<form action="QRR.php" method="POST">
<div class="blog-post blog-single-post">
<div class="single-post-title">
<h2>Post Your Answer</h2>
</div>
<div align="Right">
<textarea class="form-control" rows="4" name ="answer" id="Test">
</textarea>
<br>
<div class="actions">
<?php echo '<input type="button" class="btn btn-large " value="Post Answer" onclick="window.location=\'QRR.php?start=' . urlencode($_GET['start']) . ' \';" />'; ?>
</div>
</div>
</div>
</form>
QRR.php
<?php
// variables
$answer=$_REQUEST['answer'];
require ("coonection.php");
$FURL = $_REQUEST['hi'];
//query
$query = "INSERT INTO `answers`(`answer_id`, `question_id`, `answer`, `answerer`, `rate`, `dnt`) VALUES ('','$FURL','$answer','ahmed','',CURRENT_TIMESTAMP)";
$data=mysql_query($query) or die(mysql_error());
if($data)
{
echo "Your Questions Has Been Successfully Added ";
}
?>
if i removed passing hi from QR to QRR answer stored //$answer
if i removed storing answer from the text area the id from url stroed //$FURL
This isnt the most beautiful code, as i just copied yours and made a few modifications.. but this form will submit back to the same page, and the php will run and insert ONLY if the form is submitted.
if(isset($_POST['answer'])) says "if the variable _POST answer is set, run the php code and insert.. if it is not set, do nothing.
You will notice the form action is left blank, you can set it to the page name yo are on.. as the form will send the variables to the same page. This is a good way of doing it because if there are errors, you can prepopulate the input or textareas with the code they just typed in.
<?php
// variables
if(isset($_POST['answer'])){
$answer=$_POST['answer'];
require ("coonection.php");
$FURL = $_POST['hi']; // there is no input name 'hi' set in your form. so this code will fail due to that.
//query
$query = "INSERT INTO `answers`(`question_id`, `answer`, `answerer`, `rate`, `dnt`) VALUES ('$FURL','$answer','ahmed','',CURRENT_TIMESTAMP)";
$data=mysql_query($query) or die(mysql_error());
if($data){
echo "Your Questions Has Been Successfully Added ";
}
}
?>
`
you will see i removed your answer_id. if you use this code, make sure that field is set to primary auto increment in your database.
<form action="" method="POST">
<div class="blog-post blog-single-post">
<div class="single-post-title">
<h2>Post Your Answer</h2>
</div>
<div align="Right">
<textarea class="form-control" rows="4" name="answer" id="Test"></textarea>
<br>
<div class="actions">
<button type="submit" class="btn btn-large " value="Post Answer">Post Answer</button>
</div>
</div>
</div>
</form>
NOTE: both of these snippets will go in the same page.

How to get form input array into PHP array but not empty field

I have a form like the one below which is posted to same page, and the user can dynamically add more textbox with jquery.
<h1> Add Your Order </h1>
<form method="post" action="">
<p id="add_field"><span> Click to Add </span></p>
<div id="container">
<input type="text" class="pid" id="' + counter + '" name="Product[]" />
<input type="text" class="qid" id="' + counter + '" name="Quantity[]" /><br />
</div><br />
<input type= "submit" Name="submit_order" value="Submit">
</form>
everything work fine but if someone add more textbox and leave some textbox as empty then it going to submit. this is my problem i don't want to submit empty textboxes in my table and i want server side solution for this.
Here is my full code with php
<body>
<?php
if ( isset($_POST['submit_order']) ) {
if ( !empty($_POST['Product']) && !empty($_POST['Quantity']) ) {
$product = ($_POST['Product']);
$quantity = ($_POST['Quantity']);
foreach ($product as $id => $value) {
$products = ($product[$id]);
$quantitys = ($quantity[$id]);
$query = mysql_query("INSERT iNTO myorders (product,quantity) VALUES ('$products','$quantitys')", $connection);
}
}
echo "<i><h2><stront>" . count($_POST['Product']) . "</strong> Entry Added </h2></i>";
mysql_close();
}
?>
<?php
if (!isset($_POST['submit_order'])) {
?>
<h1> Add Your Order </h1>
<form method="post" action="">
<p id="add_field"><span> Click to Add </span></p>
<div id="container">
<input type="text" class="pid" id="' + counter + '" name="Product[]" />
<input type="text" class="qid" id="' + counter + '" name="Quantity[]" /><br />
</div><br />
<input type= "submit" Name="submit_order" value="Submit">
</form>
<?php }
?>
</body>
You can use array_filter to get the elements of the arrays that are not empty. If the number of non-empty elements is different from the original array sizes, the user left some fields blank.
$filled_product = array_filter($product);
$filled_quantity = array_filter($quantity);
if (count($filled_product) < count($product) || count($filled_quantity) < count($quantity)) {
// Report error because of unfilled fields
}
Something like this should work:
foreach ($_POST[Product] as $key => $value):
if (empty($value)):
unset($_POST[Product][$key]);
endif;
endforeach;
foreach ($_POST[Quantity] as $key => $value):
if (empty($value)):
unset($_POST[Quantity][$key]);
endif;
endforeach;
Thank you everyone for helping me, unfortunately no one give me a complete solution for this question
but #Wranorn give me a idea and i change my code and yes this solve my question
here is my solution for this
<body>
<?php
if ( isset($_POST['submit_order']) ) {
$product = ($_POST['Product']);
$quantity = ($_POST['Quantity']);
foreach ($product as $id => $value) {
$products = ($product[$id]);
$quantitys = ($quantity[$id]);
if (!empty($products) && !empty($quantitys)) {
$query = mysql_query("INSERT iNTO myorders (product,quantity) VALUES ('$products','$quantitys')", $connection);
}
}
echo "<i><h2><stront>" . count($_POST['Product']) . "</strong> Entry Added </h2></i>";
mysql_close();
}
?>
<?php
if (!isset($_POST['submit_order'])) {
?>
<h1> Add Your Order </h1>
<form method="post" action="">
<p id="add_field"><span> Click to Add </span></p>
<div id="container">
<input type="text" class="pid" id="' + counter + '" name="Product[]" />
<input type="text" class="qid" id="' + counter + '" name="Quantity[]" /><br />
</div><br />
<input type= "submit" Name="submit_order" value="Submit">
</form>
<?php }
?>
</body>

Categories