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.
Related
I am trying to create a quiz, in order to do this I am using a form which users will input information into, once they submit this form it should add the input to an array/list.
The user should then be able to enter information and the process would repeat.
The finished product would be an array with each element corresponding to the order the answers were given.
-
I have tried so far using both array_push() and declaring elements, eg: $my_array[0] = $input;.
The current problem I am experiencing is that each time I submit the form, the $count variable doesn't seem to increment.
Instead it simply stores the data in the first element and overwrites which was previously there.
I am inclined to believe this is a problem with the posting of the submit button.
-
Here is my code:
<html>
<body>
<form action="" method="POST">
<input type="text" name="INPUT" placeholder="Input something"; required /><br><br>
<input type="submit" name="Submit" /><br><br>
<?PHP
$my_array = array();
$count = 0;
if(isset($_POST['Submit'])){
global $count;
$input = $_POST['INPUT'];
$my_array[$count] = $input;
print_r($my_array);
echo "Count:" . $count;
$count++;
}
?>
</form>
</body>
</html>
The crux of the issue here is that variable values do not persist across PHP requests. Every time you submit the form, you are throwing away your old $count and $my_array variables and initializing new variables with the same names.
Here is a working version of your code snippet, which takes advantage of the PHP $_SESSION variable to have persistent information between requests:
<?php
session_start();
if (!isset($_SESSION["my_array"])) {
$_SESSION["my_array"] = array();
}
?>
<html>
<body>
<form action="" method="POST">
<input type="text" name="INPUT" placeholder="Input something"; required /><br><br>
<input type="submit" name="Submit" /><br><br>
<?php
if(isset($_POST['Submit'])){
array_push($_SESSION["my_array"], $_POST['INPUT']);
print_r($_SESSION["my_array"]);
echo "Count:" . count($_SESSION["my_array"]);
}
?>
</form>
</body>
</html>
So i got this code, at the moment it is repeating everything , and i just wanted it to repeat the echo, so i get all usernames from it, if i leave it as it is it will also repeat the form when i press a username. Every time i tried to ajust it, it just gave me syntax errors
<?php do { ?>
<?php
$username = $row_mensagens['username'];
$user = $row_mensagens['id'];
if(isset($_GET['user']) && !empty($_GET['user'])){
?>
<form>
Introduz mensagem : <br>
<textarea name='message' rows='7' cols='60'></textarea>
<br><br>
<input type='submit' value="Send Message" />
</form>
<?php
} else {
echo "<p><a href='mensagens.php?user=$user'>$username</a></p>";
}
?>
<?php } while ($row_mensagens = mysql_fetch_assoc($mensagens)); ?>
that do { } while() will always repeat as many as the number of records come from database.
You can do it this way:
<?php
if(isset($_GET['user']) && !empty($_GET['user'])){
?>
<form>
<input type="hidden" name="user" value="<?php echo $_GET['user']; ?>" /> <!-- hidden field so you can process to who -->
Introduz mensagem : <br>
<textarea name='message' rows='7' cols='60'></textarea>
<br>
<br>
<input type='submit' value="Send Message" />
</form>
<?php
} else {
do {
$username = $row_mensagens['username'];
$user = $row_mensagens['id'];
echo "<p><a href='mensagens.php?user=$user'>$username</a></p>";
} while ($row_mensagens = mysql_fetch_assoc($mensagens));
}
?>
Move do { inside else and show the form only if you have a $_GET['user']
I have also added for you a hidden field, so you know who to send message.
Hope you understand how this works. Documentation on Control Structures: do-while
I also suggest to make that form a post form, as by default it is a get form, and since you have a textarea you are more likely to bump into errors if the message is too long.
LE: Another suggestion, try to move to PDO or mysqli_* functions since mysql_* functions are considered deprecated as of PHP 5.5 and have some good chances to be removed.
First, thanks for taking a look at this. I am trying to create an array of forms that acts as a dynamically sized results list. From the results that were given the user can click 'detail' (a submit button) to get further information on the result which is why I am attempting to create an array of forms. Here is what I had tried, which compiled but the buttons aren't doing anything. Any help would be great :)
<?php
session_start();
?>
<html>
<head>
</head>
<body>
<?PHP
$numbers=array(1,2,3,4,5);
$listsize=count($numbers);
for($currentnum=0;$currentnum <$listsize;$currentnum ++){
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<input type="button" value="Submit" name="button<?PHP echo $currentnum?>" />
</form>
<?PHP
echo "<br/>";
}
if(isset($_POST['button'.$currentnum])){
echo "You choose ".$currentnum;
}
?>
</body>
</html>
This is really just meant to demonstrate what I am trying to do (thought that would be easier without functions out of scope of question).
Try changing the HTML for the button:
<input type="button" value="Submit" name="button<?PHP echo $currentnum?>" />
Should be:
<input type="submit" value="Submit" name="button<?PHP echo $currentnum?>" />
Close bracket for your for loop so that your check is inside it:
<?PHP
echo "<br/>";
}
if(isset($_POST['button'.$currentnum])){
echo "You choose ".$currentnum;
}
?>
Should be:
<?php
echo "<br/>";
if(isset($_POST['button'.$currentnum])){
echo "You choose ".$currentnum;
}
}
?>
If you learn to indent your code you'll find these kinds of bugs much easier to spot!
Other than that you're good to go...
You need to change your input types from 'button' to 'submit' so that they submit the forms, then you need to move
if(isset($_POST['button'.$currentnum])){
echo "You choose ".$currentnum;
}
inside of the for loop
I am new to PHP and I was making this form and I wanted to print some data but it is not displaying. What is wrong with it? Here's the code:
<form name="input" action="check.php" method="get">
Unit number:
<input type="number" name="unit" />
<input type="submit" value="Submit" />
</form>
<table>
<tr><td class="check-table">
<?php
if($_GET[unit] = null) $output="<p>Please Enter A Unit Number</p>";
echo $output;
?>
</td></tr></table>
Please Help?
The better way would be:
if (empty($_GET['unit'])) {
$output="<p>Please Enter A Unit Number</p>";
echo $output;
}
The reasons:
You check if variable exists
You use ' quotes for array key name
You output $output variable only if it is necessary. And in your case - you output it even if it doesn't exist
You've also confused == (comparison operator) and = (assignment operator)
I think you missed the single quotes in the $_GET['unit']
<?php
if($_GET['unit'] = null) $output="<p>Please Enter A Unit Number</p>";
echo $output;
?>
i am writing a unit converter php program. i have the set up of the page, but it seems like my php file is not being found. when i click the submit button i am brought a an error page. this is my html code.
<html>
<head>
</head>
<body>
<form action="hw7.php" method="post">
<h2>Convert length:</h2>
<p>Select conversion direction: <br />
<input type="radio" name="dir" value="1"
checked="checked"/> Feet to meters<br />
<input type="radio" name="dir" value="2" /> Meters to feet<br
/>
</p>
<p>Value to be converted: <br /><input type="text"
name="cvalue" /></p>
<p><input type="submit" value="Convert" /></p>
</form>
</body>
</html>
and here is my php file
<?php
$fTOm = $_POST["cvalue"] * 3.2808;
$mTOf = $_POST["cvalue"] / 3.2808;
echo "Result: ";
if ($POST[ 'dir'] == "1") <?php echo "$_POST["cvalue"]; ?> feet = <?php echo "fTOm"; ?> meters;
?>
Your PHP script is likely causing a server error due to improper coding. Check your server logs for PHP related errors, and possibly turn on error_reporting for PHP.
Within your PHP script, you have a statement of:
if ($POST[ 'dir'] == "1") <?php echo "$_POST["cvalue"]; ?> feet = <?php echo "fTOm"; ?> meters;
Since you're already in a PHP script, why are you using the <?php echo inline an if statement?? Correct this issue and try running the script by directly calling it.
Change the if line to :
if ($POST[ 'dir'] == "1") {
echo $_POST["cvalue"] . "feet , " . $fTOm . " meters";
}
You should know that the hw7.php file must be in the same directory as the HTML page that is calling it from a form.
I will recommend to use always relative URLs or the contexT_path when possible.
try:
<form action="./hw7.php" method="post">
try this maybe it will help:
<form action="./hw7.php" method="post">
Also remove all those syntax errors in the php script.<?php .... ... ?> can not have another <?php inside of it.