<?php
$score=0;
$i=0;
function icremm($i) {
$Question1=array( "answer1aa","answer1b","answer1c","answer1d");
$Question2=array( "answer2a","answer2bb","answer2c","answer2d");
$Question3=array( "answer3a","answer3b","answer3c","answer3dd ");
$allquest=(array($Question1,$Question2,$Question3));
$rand1=$allquest[$i];
?>
<form method="POST">
<input type="submit" name="1b" value="<?php echo $rand1[0] ?>"/>
<input type="submit" name="2b" value="<?php echo $rand1[1] ?>" />
<input type="submit" name="3b" value=" <?php echo $rand1[2] ?>" />
<input type="submit" name="4b" value="<?php echo $rand1[3] ?>" />
</form>
<?php
echo "<br>";
global $score;
echo $score;
echo "<br>";
echo "<br>";
global $i;
echo $i;
}
icremm($i);
if (isset($_POST["1b"]) ) {
$score+=5;
$i++;
goto L1;
}
if( $i==0 && isset($_POST["2b"]) || $i==0 && isset($_POST["3b"]) ||$i==0 && isset($_POST["4b"]) ) {
header("Location: lost.php");
}
L1:{
#ob_end_clean();
icremm($i);
if (isset($_POST["2b"]) ) {
$score+=5;
$i++;
#ob_end_clean();
echo"you won";
}
}
?>
i try to make my first game like "who wants to be a millionaire" and when i answer first question and go to L1 the
first statement(//if( $i==0 && isset($_POST["2b"]) || $i==0 && //isset($_POST["3b"]) ||$i==0 && isset($_POST["4b"]) ) {
// header("Location: lost.php");) still works
but i want to L1 statement to work
Related
I have a for loop that displays radio buttons and I want the first one to display as checked. But when I put a if statement inside the for loop for this the page nevers loads. Any ideas?
$mains = array(0=>'Beef Steak', 1=>'Chicken Breast', 2=>'Pork Chops');
$mainscount = count($mains);
<?php for ($mainNO = 0; $mainNO < $mainscount; $mainNO++) { ?>
<label for="mains<?php echo $mainNO ?>" class="radiobutton"><?php echo $mains[$mainNO]; ?></label>
<input type="radio" name="mains" id="mains<?php echo $mainNO; ?>" value="<?php echo $mainNO; ?>"
<?php if($mainNO = 0){ echo 'checked="checked"'; } ?>/>
<?php } ?>
<?php for ($mainNO = 0; $mainNO < $mainscount; $mainNO++) { ?>
<label for="mains<?php echo $mainNO ?>" class="radiobutton"><?php echo $mains[$mainNO]; ?></label>
<input type="radio" name="mains" id="mains<?php echo $mainNO; ?>" value="<?php echo $mainNO; ?>"
<?php if ($mainNO == 0) {
echo ' checked="checked" ';
} ?>/>
<?php } ?>
you use = where you should use ==
u are using assingment operator in comparision statement
<?php for ($mainNO = 0; $mainNO < $mainscount; $mainNO++) { ?>
<label for="mains<?php echo $mainNO ?>" class="radiobutton"><?php echo $mains[$mainNO]; ?></label>
<input type="radio" name="mains" id="mains<?php echo $mainNO; ?>" value="<?php echo $mainNO; ?>"
<?php if ($mainNO == 0) {
echo " checked";
} ?>/>
and in HTML5 you can use checked only
<input type="checkbox" checked>
// Note: You used single = in if condition that is wrong, it will create indefinite loop . Tested code.
$mains = array(0=>'Beef Steak', 1=>'Chicken Breast', 2=>'Pork Chops');
$mainscount = count($mains);
for ($mainNO = 0; $mainNO < $mainscount; $mainNO++) {
// Checked if value is 0
if($mainNO == 0){ $checked = 'checked="checked"'; }else { $checked =''; };
echo "<label for='mains".$mainNO."' class='radiobutton'>".$mains[$mainNO]."</label>";
echo "<input type='radio' name='mains' id='mains".$mainNO."' value='".$mainNO."' $checked />";
}
Working on a simple php code. When it press on only PH it show hello, and only on chlorine it show yello. When both is pressed it show sello.
<?php
if(isset($_POST['submit'])){
foreach($_POST['verdi'] as $animal){
if(isset($_POST['verdi[]==PH']))
{
echo "hello";
}
}
}
?>
<form name="input" action="" method="POST">
<input type="checkbox" name="verdi[]" value="PH">PH<br>
<input type="checkbox" name="verdi[]" value="Chlorine">Chlorine<br>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
You can do a simple check in PHP:
if( in_array("PH", $_POST["verdi"]) ){
echo "in array!";
}
if(isset($_POST['submit']) && is_array($_POST['verdi'])) {
$checked = array();
foreach($_POST['verdi'] as $animal) {
// you can do extra validation here.
$checked[] = $animal;
}
if(in_array("PH", $checked) && in_array("Chlorine", $checked)) {
echo "sello";
} else {
if(in_array("PH", $checked)) {
echo "hello";
} else if(in_array("Chlorine", $checked)) {
echo "yello";
}
}
}
I have a result(string) of 1,1,0,0 - These come from $sub_array['state']
Currently all of my check boxes are checked. How can I code the code below so that if its 1 its checked else its not? as the current code gives them all 'checked'
<?php
foreach($assoc_categories as $sub_array)
{
if($sub_array['state'] == 1)
{
$checked_state = " checked='checked'";
}
?>
<div>
<input
class="checkbox"
type="checkbox"
name="product_category"
class="product_category_selector"
id="product_category_<?php echo $sub_array['cat_id']; ?>"
data-id="<?php echo $sub_array['cat_id']; ?>"
<?php echo $checked_state; ?>
/>
<?php echo $sub_array['name']; ?>
</div>
<input
class="order"
type="input"
value="<?php echo $sub_array['sorder']; ?>"
/>
<?php
}
?>
Change:
if($sub_array['state'] == 1)
{
$checked_state = " checked='checked'";
}
To:
if($sub_array['state'] == 1)
{
$checked_state = " checked='checked'";
} else
{
$checked_state = "";
}
Basically, you are not clearing the previous value as the loop continues.
Alternatively, you could use:
$checked_state = ($sub_array['state'] == 1) ? " checked='checked'" : "" ;
You forget to reset checked_state or reset it to '' if $sub_array['state'] is equal to 0.
<?php
$assoc_categories = array(
array('state'=>1, 'cat_id'=>1, 'name'=>'one', 'sorder'=>1),
array('state'=>1, 'cat_id'=>2, 'name'=>'three', 'sorder'=>2),
array('state'=>0, 'cat_id'=>3, 'name'=>'four', 'sorder'=>3),
array('state'=>0, 'cat_id'=>4, 'name'=>'five', 'sorder'=>4),
);
foreach($assoc_categories as $sub_array)
{
$checked_state = $sub_array['state'] == 1 ? " checked='checked'" : '';
?>
<div>
<input
class="checkbox"
type="checkbox"
name="product_category"
class="product_category_selector"
id="product_category_<?php echo $sub_array['cat_id']; ?>"
data-id="<?php echo $sub_array['cat_id']; ?>"
<?php echo $checked_state; ?>
/>
<?php echo $sub_array['name']; ?>
</div>
<input
class="order"
type="input"
value="<?php echo $sub_array['sorder']; ?>"
/>
<?php
}
I'm doing form in php but I have some problem.
First I will have 3 different form in the same page.
What I want is only 1 form appear and then with the answer a second form will appear and so on.
The answer of the form will be display on the same page.
For now my first form work and after get the answer go to the 2nd form but I want to submit the 2nd form the problem appear.
It delete the answer of my first form and don't do anything (everything start like I am in my first form).
I try to find the problem but can't have idea about how to solve it.
Here is my code:
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
Q1?
<input type="number" name="nbtemplate" min="1" max="30">
<input type="submit" name="submitbutton1" value="Confirm!">
</form>
<?php
if(!isset($submitbutton1)) {
if (!empty($_POST['nbtemplate']) != "") {
echo "<b>{$_POST['nbtemplate']}</b> !\n";
echo "<br />";
$Nnbtemplate = $_POST['nbtemplate'];
$result = mysql_query("UPDATE tb SET day='$Nnbtemplate'") or die(mysql_error());
?>
<form action='<?php echo $_SERVER['PHP_SELF'];?>' method='post'>
Q2? <br>
<?php
for ($i = 1; $i <= $Nnbtemplate; $i++) { // start loop
echo "Template ";
echo $i;
?>
<input type="number" name="nbtime" min="1" max="96">
<?php
}
echo '<input type="submit" name="submitbutton2" value="Confirm!">';
echo '</form>';
if(isset($submitbutton1) && !isset($submitbutton2)) {
if (!empty($_POST['nbtime']) != "") {
echo "<b>{$_POST['nbtime']}</b> !\n";
echo "<br />";
$nbtime = $_POST['nbtime'];
for ($j = 1; $j <= $nbtime; $j++) {
echo "Time";
echo $j;
?>
Q3:
<input type="time" name="starttime"> To <input type="time" name="endtime">
<?php
}
echo '<input type="submit">';
echo '</form>';
}
}
}
}
?>
That is some gnarly code you got there, brother. This is a really simple task when handled with some javascript. Then you can have a single post to your php. I like using the jQuery framework so here's a couple links I found quickly: this one and this one
Example code in response to comment about building form elements dynamically:
<html>
<head>
<!-- load jquery library -->
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
</head>
<body>
<form action="toyourpage.php">
How Many?:
<input type="text" name="number" id="number">
<div id="add"></div>
</form>
<!-- javascript go -->
<script type="text/javascript">
$(document).ready(function()
{
$('input#number').keyup(function()
{
var num = $(this).val(); // get num
if(!isNaN(num)) // check if number
{
$('div#add').html(''); // empty
for(i = 1; i <= num; i++) // add
{
$('div#add').append('New Field ' + i + ': <input type="text" name="next_' + i + '" id="next' + i + '"><br>');
}
}
else
{
alert('Valid number required');
}
});
});
</script>
</body>
</html>
I did some changes on Your code, and have tested, it works.
You had any mistakes in your {} brackets and if conditions. Also as I commented I added extract($_POST).
<?php
extract ( $_POST );
if (! isset ( $submitbutton1 ) && !isset($submitbutton2)) {
?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
Q1? <input type="number" name="nbtemplate" min="1" max="30"> <input
type="submit" name="submitbutton1" value="Confirm!">
</form>
<?php ;
}
if (isset ( $submitbutton1 )) {
if (! empty ( $_POST ['nbtemplate'] ) != "") {
echo "<b>{$_POST['nbtemplate']}</b> !\n";
echo "<br />";
$Nnbtemplate = $_POST ['nbtemplate'];
$result = mysql_query("UPDATE tb SET day='$Nnbtemplate'") or
die(mysql_error());
?>
<form action='<?php echo $_SERVER['PHP_SELF'];?>' method='post'>
Q2? <br>
<?php
for($i = 1; $i <= $Nnbtemplate; $i ++) { // start loop
echo "Template ";
echo $i;
?>
<input type="number" name="nbtime" min="1" max="96">
<?php
}
echo '<input type="submit" name="submitbutton2" value="Confirm!">';
echo '</form>';
}
}
if ( isset ( $submitbutton2 )) {
if (! empty ( $_POST ['nbtime'] ) != "") {
echo "<b>{$_POST['nbtime']}</b> !\n";
echo "<br />";
$nbtime = $_POST ['nbtime'];
for($j = 1; $j <= $nbtime; $j ++) {
echo "Time";
echo $j;
?>
Q3:
<input type="time" name="starttime"> To <input type="time"
name="endtime">
<?php
}
echo '<input type="submit">';
echo '</form>';
}
}
?>
I'm trying to make a simple script that allow formatting text and submit it.
Here is the form:
<html>
<head>
<title>
</title>
</head>
<body>
<form method="post" action="step2.php">
<input type="text" name="text" /><br>
Red<input type="radio" name="Red" /> <br>
15px<input type="radio" name="15" /> <br>
<input type="submit" name="submit"/>
</form>
</body>
</html>
and in the step2.php at this stage i'm showing results when 2 options are selected. I'm trying to show results when only "Red" is select, when only "15px" is selecet, when both are selected and when nothing is selected.
Here is my script for the moment:
<?php
if (isset($_POST['Red']) && isset($_POST['15']) ) {
echo "<font size='15px' color=red>";
echo $_POST['text'];
echo "</font>";
}
?>
I succeeded
Thanks for answers!
the secret was in empty($varname), here's the code
<?php
if (isset($_POST['Red']) && isset($_POST['15']) ) {
echo "<font size='15px' color=red>";
echo $_POST['text'];
echo "</font>";
}
if (empty($_POST['Red']) && isset($_POST['15']) ) {
echo "<font size='15px'>";
echo $_POST['text'];
echo "</font>";
}
if (isset($_POST['Red']) && empty($_POST['15']) ) {
echo "<font color=red>";
echo $_POST['text'];
echo "</font>";
}
if (empty($_POST['Red']) && empty($_POST['15']) ) {
echo $_POST['text'];
}
?>
I think it's better way to do it is some XML/DOM tool
But you can use this code:
$attrs='';
if(isset($_POST['Red']))
$attrs.='color=red';
if(isset($_POST['15']))
$attrs.='size="15px";
Besides, you should know that <font> is deprecated now.
Radiobuttons should have the same name, otherwise use checkbox, and also better to use not numeric names for form fields
<html>
<head>
<title>
</title>
</head>
<body>
<form method="post" action="step2.php">
<input type="text" name="text" /><br>
Red<input type="checkbox" name="Red" value="Red" /> <br>
15px<input type="checkbox" name="px15" value="15" /> <br>
<input type="submit" name="submit"/>
</form>
</body>
</html>
step2.php
<?php
if (isset($_POST['Red']) && isset($_POST['px15']) ) {
echo "<font size='15px' color=red>";
echo $_POST['text'];
echo "</font>";
}
?>
Here is a solution :) :
<?php
if (isset($_POST['Red']) && isset($_POST['15']) ) {
echo "<font size='15px' color=red>";
echo $_POST['text'];
echo "</font>";
}
if (empty($_POST['Red']) && isset($_POST['15']) ) {
echo "<font size='15px'>";
echo $_POST['text'];
echo "</font>";
}
if (isset($_POST['Red']) && empty($_POST['15']) ) {
echo "<font color=red>";
echo $_POST['text'];
echo "</font>";
}
if (empty($_POST['Red']) && empty($_POST['15']) ) {
echo $_POST['text'];
}
?>