The following PHP code is for generating a random number of four digits ($numero) and using it as a validation for a simple HTML form with three input boxes. The last input box is for entering the code (the random number). If the user doesn't write the right number in that last input box the program skips its purpose, which is adding some text to a database (agregar.txt). I think the code is fine, except for if ($_POST['password'] != $numero) {. Should I change it to a string or use another kind of variable? Each time I run the code it acts as if $numero was different from password, and I'm sure I'm writing the right number. Please some help.
<html>
<body>
<center>
<h2>Agregar entradas a diccionarioie</h2>
<?php
// RANDOM FOUR DIGITS NUMBER
$numero = rand(1000, 9999);
echo "<b>Código: </b><big>".$numero."</big><p>";
if ($_POST['password'] != $numero) {
?>
<form name="form" method="post" action="">
<input title=" LEMA " size="30" type="text" name="lema" autofocus><br>
<input title=" TRADUCCIÓN " size="30" type="text" name="trad"><br>
<input title=" CÓDIGO " size="30" type="text" name="password"><br>
Gracias por colaborar <input title=" ENVIAR " type="submit" value="•"></form>
<?php
} else {
$lema = $_POST['lema'];
$trad = $_POST['trad'];
// ADDING TEXT TO A DATABASE
$texto = $lema." __ ".$trad."\n";
$docu = fopen("agregar.txt", "a+");
fwrite($docu, $texto);
fclose($docu);
}
?>
</center>
</body>
</html>
As pointed out by #Fred -ii-, the problem in your code is the $numero get generated to different random number when you submit the form. The solution is to use session: PHP session example
The session can be used to store your $numero value after the form being submitted. Here's the updated code:
<?php
// Make sure to start the session before any output.
session_start();
if (isset($_POST['password']) && isset($_SESSION['numero']) && $_POST['password'] == $_SESSION['numero']) {
unset($_SESSION['numero']);
$lema = $_POST['lema'];
$trad = $_POST['trad'];
// ADDING TEXT TO A DATABASE
$texto = $lema." __ ".$trad."\n";
$docu = fopen("agregar.txt", "a+");
fwrite($docu, $texto);
fclose($docu);
} else {
// RANDOM FOUR DIGITS NUMBER & STORE IT IN SESSION.
$numero = $_SESSION['numero'] = rand(1000, 9999);
?>
<html>
<body>
<center>
<h2>Agregar entradas a diccionarioie</h2>
<b>Código: </b><big><?php echo $numero; ?></big><p>
<form name="form" method="post" action="">
<input title="LEMA " size="30" type="text" name="lema" autofocus><br>
<input title="TRADUCCIÓN " size="30" type="text" name="trad"><br>
<input title="CÓDIGO " size="30" type="text" name="password"><br>
Gracias por colaborar <input title=" ENVIAR " type="submit" value="•">
</form>
</center>
</body>
</html>
<?php }
Just make sure that you call session_start() before any output (in your case the HTML document).
Hope this help.
Related
i have a file 'colecting-data.html' with this code :
<html>
<body>
<form action="collect.php" method="get">
name : <input type="text" name="name">
email : <input type="text" name="email">
telephone: <input type="text" name="telephone">
<input type="submit" value="save">
</form>
</body>
</html>
i when to creat "collect.php" file to collect all the data (name email telephone)
and save then in another file "save.html" inside a table .
it will help me a lot if any one know how to do that .
Very quick & dirty answer, remember to sanitize your inputs.
<?php
//collect.php
$html = "<html><head><title>Test</title></head><body><table>";
foreach($_REQUEST as $k=>$v) {
$html .= "<tr><td>$k</td><td>$v</td></tr>";
}
$html .= "</table></body></html>";
file_put_contents('save.html',$html);
?>
As you can use this to learn, start with the basic way to do this before going for more advanced options...
First off, your form needs to be a POST, not a GET.
<form action="collect.php" method="POST">
This will POST the values to collect.php.
On collect.php, you'll need to collect the values;
<?php
if(isset($_POST['name']) {
// Do Something
;}
if(isset($_POST['email']) {
// Do Something
;}
if(isset($_POST['telephone']) {
// Do Something
;}
?>
From here, you can set the posted values as variables and then do whatever you want with them.
Newbie here, and self-taught in PHP. I have a questionnaire where each question has 2 answers, and users can add any combination of numbers to each, as long as they equal, i.e. 10. So far 2 questions (will be more) so each question's answers should equal 10, therefore total submitted values should equal 20. I can't find a way to only allow submit if these conditions are met. I would really appreciate any help.
Currently using this for testing purposes:
<input type="submit" value="Check!" name="check"/>
...at the bottom.
<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<?php
// Adding stuff
if(isset($_POST['check']))
{
$q1total=$realist[1]+$idealist[1];
$q2total=$realist[2]+$idealist[2];
$grandtotal=$q1total+$q2total;
}
?>
<body>
<form method="post">
Q1: <input type="text" name="realist[1]"/> <input type="text" name="idealist[1]"/>
<?php echo $q1total; ?>
<br>
Q2: <input type="text" name="realist[2]"/> <input type="text" name="idealist[2]"/>
<?php echo $q2total; ?>
<br>
<br><br>
Grand total: <?php echo $grandtotal; ?>
<br><br>
<input type="submit" value="Check!" name="check"/>
</form>
</body>
</html>
I'm also a self-taught PHP "newbie" as you describe it. I would honestly create a seperate PHP file that checks if the conditions are met.
Your form would be:
<form method="post" action="yourfile.php">
And in the php file:
$realist1 = $_POST["realist\[1\]"];
$idealist1 = $_POST["idealist\[1\]"];
$realist2 = $_POST["realist\[2\]"];
$idealist2 = $_POST["idealist\[2\]"];
if ($realist[1]+idealist[1]== 10 && $realist[2]+idealist[2] == 10)
{
echo "<button value='correct'/>";
}else{
echo "<button value='incorrect' disabled/>";
}
Obviously you have to adjust some stuff but hopefully this can help.
How can I assign a variable to a specific date for use in a form?
I want a warning to appear when the user tries to book too many a people on a specific date. I started writing this code but didn't know where to go from there:
<?php
$spaces = 20;
$num_people = GET('#people');
$message = "Unfortunately we don't have this many spaces avilable on this date. We have a maximum of $spaces.";
if($spaces < $num_people) {
echo $message;
}
?>
How could I assign the value $spaces to a specific date and link it into a form? Here is the form:
<form method="post">
Name:<br>
<textarea id="name"></textarea><br>
<br>
Date leaving:<br>
<br>
<textarea id="date"></textarea><br>
<br>
How many people?:<br>
<textarea id="people"></textarea><br>
<br>
<input type="submit">
$num_people = GET('#people');
This line should be
$num_people = $_GET['people'];
and
<textarea id="people"></textarea><br>
shoul be
<textarea id="people" name="people"></textarea><br>
And your method ahould be GET. Since you are sending GET request.
Your code should look like this:
<?php
if(isset($_GET['people'])){
$spaces = 20;
$num_people = $_GET['people'];
$message = "Unfortunately we don't have this many spaces avilable on this date. We have a maximum of $spaces.";
if($spaces < $num_people) {
echo $message;
}
}
?>
<form method="get" action="">
Name:<br>
<textarea id="name"></textarea><br>
<br>
Date leaving:<br>
<br>
<textarea id="date"></textarea><br>
<br>
How many people?:<br>
<textarea id="people" name="people"></textarea><br>
<br>
<input type="submit">
I've attempted to make a square root calculator with PHP and HTML using form. but it won't seem to get the output statement. Here it is:
<?php
$num = $_POST['getroot'];
$pull_sqrt = print(sqrt($num));
print("The Square root of "$num"is "$pull_sqrt);
?>
<form action="root.php" method="post">
<input type="text" id="getroot" value="Number"/>
<input type="submit" id="submitroot" value="Calculate"/>
</form>
for root.php:
<?php
$num = $_POST['getroot'];
$pull_sqrt = print(sqrt($num));
print("The Square root of "$num"is "$pull_sqrt);
?>
Please help me out on explaining this, I still don't know if PHP allowed sqrt(); as a function anymore. Any way of re-editing is fine, I would like an explained way of fixing this. Thanks!
You don't have a form element named getroot
You want to do <input type="text" id="getroot" name="getroot" value="Number"/>
You cannot rely on just an id. POST requires a "named" element.
You're also missing concatenates for print("The Square root of "$num"is "$pull_sqrt);
Sidenote: Remove print from $pull_sqrt = print(sqrt($num)); otherwise it will echo as 1.
do
print("The Square root of " . $num . "is " .$pull_sqrt);
Since you're using this in one page, you need to use isset() and using action="".
<?php
if(isset($_POST['submit'])){
$num = $_POST['getroot'];
$pull_sqrt = sqrt($num);
print("The Square root of " . $num . " is " . $pull_sqrt);
}
?>
<form action="" method="post">
<input type="text" id="getroot" name="getroot" placeholder="Enter a number"/>
<input type="submit" name="submit" id="submitroot" value="Calculate"/>
</form>
You can also check if it is in fact a number that has been entered, using is_numeric().
<?php
if(isset($_POST['submit'])){
if(is_numeric($_POST['getroot'])){
$num = (int)$_POST['getroot'];
$pull_sqrt = sqrt($num);
print("The Square root of " . $num . " is " . $pull_sqrt);
// Yes, you can do the following:
$pull_sqrt = print($num * $num); // added as per a comment you left, but deleted.
}
else{
echo "You did not enter a number.";
}
}
?>
<form action="" method="post">
<input type="text" id="getroot" name="getroot" placeholder="Enter a number"/>
<input type="submit" name="submit" id="submitroot" value="Calculate"/>
</form>
I know that if i want to pass variables in php from a page to another one I can use sessions, but I don't know what is wrong with this code, it's just doesn't work,
I want to pass the $cout variable from page 1 to page 2
PAGE 1
<?php
session_start();
if(isset($_POST['date']))
{
$dat = $_POST['date'];
$matricule = $_POST['matricule'];
$kilometrage = $_POST['kilometrage'];
$num_fact = $_POST['num_fact'];
$Fournisseur = $_POST['Fournisseur'];
$num_bon = $_POST['num_bon'];
$Fonctionnaire = $_POST['Fonctionnaire'];
$cout = $_POST['cout'];
//here is all what I did
$_SESSION['cout'] = $_POST['cout'];
header("Location: PAGE2.php") ;
}
?>
and here is PAGE 2:
<?php
session_start();
$cout = $_SESSION['cout'];
echo $cout ;?>
and here is the form
<form method="post" action="page1.php" >
<input type="date" placeholder="La Date Ex: 2014-07-17" name="date"><br>
<input type="text" placeholder="Matricule" name="matricule"><br>
<input type="text" placeholder="Kilometrage" name="kilometrage"><br>
<input type="text" placeholder="Numero de facteur" name="num_fact"><br>
<input type="text" placeholder="Fournisseur" name="Fournisseur" ><br>
<input type="text" placeholder="N° de bon à delivrer au fornisseurs ou facture" name="num_bon"><br>
<input type="text" placeholder="Fonctionnaire ayant effectué la Réparation" name="Fonctionnaire" ><br>
<input type="text" placeholder="le coût de la reparation en DH" name="cout" ><br>
<input type="submit" value="Valider">
</form>`
Your code seems correct, but dependant on the existence of $_POST['date'] variable.
When you submit the form - please make sure that it is not empty.
EDIT2:
I have made two files on my own, your code works fine.
You'll need to look for error on part of server php config, or pathways.
Try following: (assuming page1.php and page2.php are in root folder of your site)
in html
<form action="/page1.php" ..
in page1
header("Location: /page2.php");
Try isset($_POST['submit']) in place of isset($_POST['date']). I think you are not filling date.
May be your temp folder is write protected. Check session_save_path() is writable or not.