I am creating form where the user can input a integer and get the multiplications (1-10) of that number. Here is my web form for user input:
<html>
<head>
<title>Assignment 9.1</title>
</head>
<body bgcolor="black" text="white">
<form method="post" action="table.php"
<strong>Enter No:</strong>
<input type="text" name="num" size="10">
<input type="submit" value="Get Table">
</form>
</body>
</html>
The table I have created is this:
<?php
$num = $_POST['num'];
if($num)
(
for ($i=1; $i<=10; $i++)
(
$mul = $num * $i;
echo "$num * $i = $mul<br>";
)
)
else
(
echo "Invalid Entry!";
)
?>
I am getting an error for the table. The error is for line 5 (FOR). I have no idea why I am getting this error. Can anyone Help?
Blocks of code are marked with curly brackets { ... }, you are currently using parentheses ( ... )
As far as I can tell, that's pretty much the only thing wrong with it. You might want to add a bit of validation:
$num = isset($_POST['num']) ? intval($_POST['num']) : 0;
But that'd just a touch-up. The brackets are your actual problem.
You need to use {} instead () in the for and if.
Like this:
<?php
$num = $_POST['num'];
if($num)
{
for ($i=1; $i<=10; $i++)
{
$mul = $num * $i;
echo "$num * $i = $mul<br>";
}
}
else
{
echo "Invalid Entry!";
}
?>
Hope it helps you!
You are using parentheses when you should be using braces. Try this...
<?php
$num = $_POST['num'];
if($num)
{
for ($i=1; $i<=10; $i++)
{
$mul = $num * $i;
echo "$num * $i = $mul<br>";
}
}
else
{
echo "Invalid Entry!";
}
?>
save the file as table.php and run working 100%
<html>
<head><title>Table</title></head>
<body>
<form action="" method="POST">
<center><input type="text" name="number" size="20" > </center> <br>
<center><input type="submit" name="table" value="get table"> </center>
</form>
</body>
</html>
<?php
$num=$_POST['number'];
if($num<=20)
{
for ($i=1; $i<=10; $i++)
{
$mul=$num*$i;
echo "<center>$mul </center><br>";
}
}
else
{
echo "<center>envalid entry</center>";
}
?>
<html>
<head><title>Table</title></head>
<body>
<form action="" method="POST">
<center><input type="text" name="number" size="20" > </center> <br>
<center><input type="submit" name="table" value="get table"> </center>
</form>
</body>
</html>
<?php
$num=$_POST['number'];
if($num<=20)
{
for ($i=1; $i<=10; $i++)
{
$mul=$num*$i;
echo "<center>$mul <br></center>";
}
}
else
{
echo "<center>envalid entry</center>";
}
?>
Related
here you see my code example:
<?php
session_start();
$arbeit = $_GET["arbeit"];
echo "Angelegte Prüfung: ";
echo $arbeit;
$damen = 0;
$herren = 0;
if (array_key_exists('add_Fem',$_POST)) {
$damen = 1; // BESETZT DAMEN
}
elseif (array_key_exists('sub_Fem',$_POST)) {
$damen = 0; // Reset counter
}
elseif (array_key_exists('add_Mal',$_POST)) {
$herren = 1; // BESETZT HERREN
}
elseif(array_key_exists('sub_Mal',$_POST)) {
$herren = 0; // Reset counter
}
?>
<form method='post'>
<input name='add_Fem' type="submit" value='Damen' class="button">
</form>
<form method='post'>
<input name='sub_Fem' type="submit" value='Reset Damen' class="button">
<h3><em>
<?php
if ($damen == 1) {
echo "DAMEN BESETZT!";
} else {
echo "DAMEN FREI!";
}
?>
</em></h3>
</form>
<form method='post'>
<input name='add_Mal' type="submit" value='Herren' class="button">
</form>
<form method='post'>
<input name='sub_Mal' type="submit" value='Reset Herren' class="button">
<h3><em>
<?php
if ($herren == 1) {
echo "HERREN BESETZT!";
} else {
echo "HERREN FREI!";
}
?>
</em></h3>
</form>
the problem is, when i'm try to set both to 1(male and female) the other variable gets reset to 0. And i dont know why they are affect each other!
Also PHP isset not solves my problem, same situation, that was my first try.
I hope you can give me some advice.
Thank you
i want make a view of array, in the first i need fill length of array for first form (this for how much is show) and then i use that to fill value of array then i show that.
<form method="POST" action="prak_9_1.php">
input length of array <input type="text" name="bilangan">
<input type="submit" value="Hasil">
second code is different file
<?php
$bilangan = $_POST['bilangan'];
$bil = $bilangan;
$array = array();
$n = 0;
?>
<form method="POST" action="prak_9_1.php">
<?php
for ($i=0; $i < $bilangan ; $i++) {
echo "input value of array : <input type='text' name='nilai_".$i."'><br>";
}
?>
<input type="submit" value="Hasil">
</form>
<?php
for ($i=0; $i < $bil ; $i++) {
$array[$n] = $_POST['nilai_'.$i];
$n++;
}
print_r($array);?>
sorry if my english is bad... thanks for ur helps...
first file :
<form method="POST" action="second.php">
input length of array <input type="text" name="bilangan">
<input type="submit" value="Hasil">
</form>
and second file :
<?php
if (isset($_POST['bilangan'])) {
echo '<form method="POST">';
for ($i = 0; $i <= $_POST['bilangan']; $i++) {
echo "input value of array : <input type='text' name='nilai[]'><br>";
}
echo '<input type="submit" value="Hasil">';
echo '</form>';
}
if (isset($_POST['nilai'])) {
print_r($_POST['nilai']);
}
?>
i want to make a multiple choice that if we choose wrong answer, then a text 'incorrect' will be displayed in side of the answer that we choose and if we choose right answer, then a text 'correct' will be displayed in side of the answer that we choose. i using radio button. please help.
this is my index.php:
<?php
include 'koneksi.php';
session_start();
$query = mysql_query("select * from t_soal") or die (mysql_error());
//$_SESSION['soal'] = mysql_fetch_array($query);
$_SESSION['soal'] = array();
$_SESSION['no'] = 1;
$_SESSION['score'] = 0;
$_SESSION['option'] = array();
$_SESSION['jawab'] = array();
$i=0;
while($row = mysql_fetch_assoc($query)){
$_SESSION['soal'][] = $row;
$_SESSION['option'][] = array($_SESSION['soal'][$i]['a'], $_SESSION['soal'][$i]['b'], $_SESSION['soal'][$i]['c'], $_SESSION['soal'][$i]['d']);
$i++;
}
if(isset($_SESSION['soal'])){
header("location:test.php");
}
this is test.php:
<?php
session_start();
$soal = $_SESSION['soal'];
$no = $_SESSION['no'];
if(isset($_POST['next'])){
$_SESSION['jawab'][] = $_POST['option'];
if($_POST['option'] == $soal[$no-2]['kunci']){
$_SESSION['score'] = $_SESSION['score'] + 10;
}
}
if(isset($soal[$no-1])){
?>
<!DOCTYPE html>
<html>
<head>
<title>Latihan Soal</title>
</head>
<body>
Kembali ke soal 1
<form action="" method="POST">
<p>
<?php
echo $no.". "; $_SESSION['no']++;
echo $soal[$no-1]['soal'];
$jawaban = $_SESSION['option'][$no-1];
shuffle($jawaban);
?>
</p>
<?php
for ($i=0; $i < 4; $i++) {
?>
<input type="radio" name="option" value="<?php echo $jawaban[$i]; ?>" required/> <?php echo $jawaban[$i]; ?></br>
<?php
}
?>
<input type="submit" name="next" value="next">
</form>
</body>
</html>
<?php
}else{
header("location:result.php");
}
?>
Assuming all your script is working already, then:
In PHP:
You will need to assign a new parameter on this script:
if($_POST['option'] == $soal[$no-2]['kunci']){
$_SESSION['score'] = $_SESSION['score'] + 10;
}
to
if($_POST['option'] == $soal[$no-2]['kunci']){
$_SESSION['score'] = $_SESSION['score'] + 10;
$_SESSION['correctAnswer'] = $soal[$no-2]['kunci']; // add sthing like this
}
and then, from
for ($i=0; $i < 4; $i++) {
?>
<input type="radio" name="option" value="<?php echo $jawaban[$i]; ?>" required/> <?php echo $jawaban[$i]; ?></br>
<?php
}
to
for ($i=0; $i < 4; $i++) {
?>
<input type="radio" name="option" value="<?php echo $jawaban[$i]; ?>" required/> <?php echo $jawaban[$i]; ?></br>
<?php
echo $jawaban[$i] == $_SESSION['correctAnswer'] ? " correct" : " incorrect";
}
In JS: (much easier)
HTML files will looks like this:
<div class="correctAnswer">
<input type="radio" name="theanswer" id="choice1" />
<label for="choice1">Choice 1</label>
</div>
<div class="incorrectAnswer">
<input type="radio" name="theanswer" id="choice2" />
<label for="choice2">Choice 2</label>
</div>
and the JS will looks like this:
$(".correctAnswer").click(function() {
$(".checks").remove();
$(this).append("<span class='checks'> correct</span>");
});
$(".incorrectAnswer").click(function() {
$(".checks").remove();
$(this).append("<span class='checks'> incorrect</span>");
});
If you want a working example, please check this fiddle link here.
Hope it helps !
in my code, when we click ok button . It's will echo 3
I want to apply to count only input that have value only
How to do ?
<?php
if(isset($_POST["submit"]))
{
echo count($_POST["to_more"]);
}
?>
<form name="f1" method="post">
<input type="text" name="to_more[]">
<input type="text" name="to_more[]">
<input type="text" name="to_more[]">
<input type="submit" name="submit" value="OK">
</form>
try this
<?php
if(isset($_POST["submit"]))
{
echo count(array_filter($_POST["to_more"]));
}
?>
How about
if(isset($_POST["submit"])){
$count = 0 ;
foreach($_POST["to_more"] as $data){
if($data != '') $count++;
}
if($count > 0)
echo $count;
}
array_filter should help
<?php
$ar = isset($_POST["to_more"]) ? $_POST["to_more"] : array();
$ar = array_filter($ar, function($el){ return !empty($el);});
echo count($ar);
?>
Should be quicker than foreach, btw.
UPD: Oh, seems, NLSaini posted the precise solution, check it.
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>';
}
}
?>