Unknown error when processing PHP form? [closed] - php

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
Below is the code for a simple form with input/output. For some reason, only question one seems to be working and questions two and three simply echo back "Something is wrong." I coded all three questions the exact same way throughout so I'm not really sure why question two and three aren't processing correctly. Any advice would be greatly appreciated.
Here is the HTML form:
<form action="processor.php" method="post">
<h4>Question # 1</h4>
<p>What grade are you in?</p>
<label class="checkbox"><input type="checkbox" name="grade" value="1"> Freshmen</label>
<label class="checkbox"><input type="checkbox" name="grade" value="2"> Sophomore</label>
<label class="checkbox"><input type="checkbox" name="grade" value="3"> Junior</label>
<label class="checkbox"><input type="checkbox" name="grade" value="4"> Senior</label>
<h4>Question # 2</h4>
<p>What is your current GPA?</p>
<select>
<option name="gpa" value="4">3.5 or above</option>
<option name="gpa" value="3">3.0-3.4</option>
<option name="gpa" value="2">2.5-2.9</option>
<option name="gpa" value="1">2.0-2.4</option>
<option>Lower</option>
</select>
<h4>Question # 3</h4>
<p>Where do you excel the most academically?</p>
<select multiple="multiple">
<option name="school" value ="1">Mathematics</option>
<option name="school" value ="2">Literature</option>
<option name="school" value ="3">History</option>
<option name="school" value ="4">Humanities</option>
<option name="school" value ="5">Science</option>
</select>
<div class="button">
<button class="btn btn-primary" input type="submit" name="submit" href="processor.php">Submit</button>
</div>
</form>
Here is the processor:
<?php
function Grades () {
$grade = $_POST['grade'];
if ($grade =="1") {
echo "You're a freshmen";
} elseif ($grade == "2") {
echo "You're a sophomore";
} elseif ($grade == "3") {
echo "You're a junior.";
} elseif ($grade == "4") {
echo "You're a senior.";
} else {
echo "Something is wrong.";
}
}
function Gpa () {
$gpa = $_POST['gpa'];
if ($gpa =="1") {
echo "You strongly need to up your GPA.";
} elseif ($gpa == "2") {
echo "You're an average student.";
} elseif ($gpa == "3") {
echo "You're an above average student.";
} elseif ($gpa == "4") {
echo "You're an excellent sudent.";
} else {
echo "Something is wrong.";
}
}
function School () {
$school = $_POST['school'];
if ($school =="1") {
echo "You're into Math";
} elseif ($school == "2") {
echo "You're into Lit";
} elseif ($school == "3") {
echo "You're into history.";
} elseif ($school == "4") {
echo "You're into humanities.";
} elseif ($school == "5") {
echo "You're into science.";
} else {
echo "Something is wrong.";
}
}
include('viewpage.php');
?>
And the view page:
<h4>Question # 1</h4>
<p><?php Grades($grade); ?></p>
<h4>Question # 2</h4>
<p><?php Gpa($gpa); ?></p>
<h4>Question # 3</h4>
<p><?php School($school); ?></p>

The problem is here
<select name="gpa">
<option value="4">3.5 or above</option>
To access $_POST['gpa'] <select> tag should have name property as gpa not <option> tag
For $_POST['School']
<select name="school" multiple="multiple">
<option value ="1">Mathematics</option>

also i think some think wrong in view page code. Grades,Gpa,School function are not have variable. but you call these function like this,
<?php Grades($grade); ?>
<?php Gpa($gpa); ?>
<?php Gpa($School); ?>
replace these code like below
<?php Grades(); ?>
<?php Gpa(); ?>
<?php Gpa(); ?>
and also you will get error if user not select options in question1 n question 2. you must add code to check that..

Related

Message after submit on radio button

I'm working on a simple quiz. One question is displayed at a time with 3 possible answers on radio buttons. The answer for each question is stored in a table from a database called
SOLUTION:
What I’m trying to do in the program is to show a message of 'Try again!' every time the user answers the question badly after clicking on SUBMIT. I have implemented it in the following code but I do not know why I always get the message of 'Try again!' when I click submit. Maybe it is because I put an 'else' after an 'empty'? Or because I'm not comparing it well with the correct variable? Can you help me find out the mistake?
This is my code:
<?php
$Err = "";
$Try = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["choice"])) {
$Err = "Please select option";
}
else{
if($_POST["choice"] != "solution_number FROM solution WHERE exercise_id_fk=$id"){
$Try = "Try again!";
}
}
}
?>
<form action='' method='post'>
<input type="radio" name="choice" value= "1" /><img src="<?php echo $row["image_path_A"] ?>"/><br>
<input type="radio" name="choice" value= "2" /><img src="<?php echo $row["image_path_B"] ?>"><br>
<input type="radio" name="choice" value= "3" /><img src="<?php echo $row["image_path_C"] ?>"><br>
<br><br>
<span class="error"><?php echo $Err;?></span>
<span class="try"><?php echo $Try;?></span>
<br><br><br><!--- Select difficulty --->
<p2>Select difficulty level:</p2>
<form action='' method='post'>
<select name="choose" id="choose">>
<option value="1" <?php if($row["difficulty"]=="1") { echo "selected"; } ?> >1</option>
<option value="2" <?php if($row["difficulty"]=="2") { echo "selected"; } ?> >2</option>
<option value="3" <?php if($row["difficulty"]=="3") { echo "selected"; } ?> >3</option>
<option value="4" <?php if($row["difficulty"]=="4") { echo "selected"; } ?> >4</option>
<option value="5" <?php if($row["difficulty"]=="5") { echo "selected"; } ?> >5</option>
</select>
<input class="buttonSubmit" type="submit" name="submit" value="Submit">
<?php
if ($next_question_id >= 0) {
?>
<a href="?id=<?php echo $next_question_id; ?>&order=<?php echo $next_question_order; ?>" class="buttonNext" >Next Question</a>
<?php
}
?>
</form>
And my PHP:
<?php
if (isset($_POST['submit'])) {
$user_id = $_SESSION['user_id'];
$user_check_query = "SELECT * FROM users WHERE id='$user_id'";
if(isset($_POST['choice'], $_POST['choose'])){
$choice_answer=$_POST['choice'];
$difficulty=$_POST['choose'];
$query = "INSERT INTO answers (exercise_id_fk, student_id, difficulty_change, difficulty_student, choice_answer, correct_answer) VALUES ('$id','$user_id', (SELECT IF(difficulty='$difficulty','NO','YES') FROM exercises WHERE exercise_id=$id), '$difficulty', '$choice_answer', (SELECT IF(solution_number='$choice_answer','1','0') FROM solution WHERE exercise_id_fk=$id))";
$sql=mysqli_query($conn,$query);
}
}
?>

Combobox in html and php

If the combobox select the Archers the browser will echo Correct but if the combobox will not select the Archers the browser will echo, i want to know if my syntax is wrong.
Wrong. Error: Undefined index text_hname.
Here's the structure:
<form action="server.php" method="POST">
<img src="images/logo.png" class="logo">
<div class="container">
<h1>Account Details</h1>
<br>
<label class="username">Username</label>
<input type="text" name="text_username" class="text_user" placeholder="Enter username">
<label class="password">Password</label>
<input type="text" name="text_password" class="text_pass" placeholder="Enter password">
<label class="email">Email</label>
<input type="text" name="text_email" class="text_email" placeholder="Enter email">
<label class="cname">Character Name</label>
<input type="text" name="text_cname" class="text_cname" placeholder="Enter Character Name">
<label class="character">Select Character</label>
<select class="names" name="text_hname">
<option>Archer</option>
<option>Barbarian</option>
<option>Balloon</option>
<option>Witch</option>
<option>Spirit</option>
<option>Hog Rider</option>
<option>Minion</option>
</select>
<img src="images/">
<br>
<input type="submit" class="submit" name="submit">
</div>
</form>
//option
$hero = $_POST['text_hname'];
if (isset($_POST['text_hname'])) {
if ($hero == 'Archers') {
echo "Correct";
} else {
echo "wrong";
}
}
The problem is that if you're trying to assign $hero before you've checked if text_hname is set, it might not be defined.
Suggested refactor:
if (isset($_POST['text_hname'])) {
$hero = $_POST['text_hname'];
if ($hero == 'Archers') {
echo "Correct";
}
else
{
echo "wrong";
}
}
//Your select in form needs values. Same values that you are going to compare.
<select class="names" name="text_hname">
<option value="Archer">Archer</option>
<option value="Barbarian">Barbarian</option>
<option value="Balloon">Balloon</option>
<option value="Witch">Witch</option>
<option value="Spirit">Spirit</option>
<option value="HogRider">Hog Rider</option>
<option value="Minion">Minion</option>
</select>
//Php code
if (isset($_POST['text_hname'])) {
$hero = $_POST['text_hname'];
if ($hero == 'Archer') { //Its Archer, not Archers
echo "Correct";
}
else
{
echo "wrong";
}
}
$_POST['text_hname'] will return the selected option value , not option name .
Modify your select like this:
<select class="names" name="text_hname">
<option value="Archer">Archer</option>
<option value="Barbarian">Barbarian</option>
<option value="Balloon">Balloon</option>
<option value="Witch">Witch</option>
<option value="Spirit">Spirit</option>
<option value="Hog_Rider">Hog Rider</option>
<option value="Minion">Minion</option>
</select>
and server.php page
if (isset($_POST['text_hname'])) {
$hero = trim($_POST['text_hname']); //** to remove any whitespace
if ($hero == 'Archer') {
echo "Correct";
} else {
echo "wrong";
}
}
EDIT : it might be because your $_POST['text_hname'] has whitespace. Try trim() on $_POST['text_hname']
Hope it's helpful.

Writing Form Submit to Text File [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I have a simple form that echos a value on submit using form. I want this value to be written to a text file, I cant seem to figure out why my code is not working any ideas?
<?php
if (isset($_POST['button1'])) {
$txt=$_POST['button1'];
file_put_contents('status.txt',$txt,FILE_APPEND|LOCK_EX);
exit();
}
?>
<form method="post" action="<?php echo $PHP_SELF;?>">
Restaurant Open:
<input type="radio" name="button1" value="Open" onClick="submit();" <?php echo ($_POST['button1'] == 'Open') ? 'checked="checked"' : ''; ?> /> Open
<input type="radio" name="button1" value="Closed" onClick="submit();" <?php echo ($_POST['button1'] == 'Closed') ? 'checked="checked"' : ''; ?>/> Closed
</form>
<?php
if (isset($_POST['button1']) == 'Open')
echo "Open Today.";
else if (isset($_POST['button1']) == 'Closed')
echo "Closed Today.";
?>
The form disappears because you exit() before writing anything.
I suggest you to use this code.
on the radio button, I changed
<?php echo ($_POST['button1'] == 'Open') ? 'checked="checked"' : ''; ?>
into
if($_POST['button1'] == 'Open') echo "checked=checked"; ?>
which will avoid misunderstood.
<?php
if (!empty($_POST['button1'])) {
$txt=$_POST['button1'];
file_put_contents('status.txt',$txt,FILE_APPEND|LOCK_EX);
//removed exit
}
?>
<form method="post" action="">
Restaurant Open:
<input type="radio" name="button1" value="Open" onClick="submit();" <? if($_POST['button1'] == 'Open') echo "checked=checked"; ?> /> Open
<input type="radio" name="button1" value="Closed" onClick="submit();" <? if($_POST['button1'] == 'Closed') echo "checked=checked"; ?> /> Closed
</form>
<?php
if ($_POST['button1'] == 'Open')
echo "Open Today.";
elseif ($_POST['button1'] == 'Closed')
echo "Closed Today.";
else
echo "Choose a status.";
?>

Issue retrieving data from DB due to undefined variable

My intention is to retrieve data from a database and have created the following functions but receive an undefined function, grades, error message. The code is as follows:
<form action="processor.php" method="post">
<h4>Question # 1</h4>
<p>What grade are you in?</p>
<label class="checkbox"><input type="checkbox" name="grade" value="1"> Freshmen</label>
<label class="checkbox"><input type="checkbox" name="grade" value="2"> Sophomore</label>
<label class="checkbox"><input type="checkbox" name="grade" value="3"> Junior</label>
<label class="checkbox"><input type="checkbox" name="grade" value="4"> Senior</label>
<h4>Question # 2</h4>
<p>What is your current GPA?</p>
<select name="gpa">
<option value="4">3.5 or above</option>
<option value="3">3.0-3.4</option>
<option value="2">2.5-2.9</option>
<option value="1">2.0-2.4</option>
<option value="0">Lower</option>
</select>
<h4>Question # 3</h4>
<p>Where do you excel the most academically?</p>
<select name="school" multiple="multiple">
<option value ="1">Mathematics</option>
<option value ="2">Literature</option>
<option value ="3">History</option>
<option value ="4">Humanities</option>
<option value ="5">Science</option>
</select>
Then the PHP file that processes the form submission:
<?php
require_once('processor.php');
class Processor {
#code
function get_response($id) {
$dsn = "mysql:host=localhost;dbname=user";
$username = "root"; // database username
$password = "stewie12"; // database password
try {
$enter = new PDO($dsn, $username, $password);
$sql = "SELECT response FROM recommendations WHERE id = ? ";
$new_item = $enter->prepare($sql);
$new_item->setFetchmode(PDO::FETCH_ASSOC);
$new_item->execute(array($id));
foreach($new_item as $nw) {
return $nw['response'];
}
} catch (Exception $e) {
echo $e->getMessage();
exit;
}
return "";
}
function GetWeird () {
$santiago="1992";
if ($santiago="1992") {
$id = 1;
} else {
$id = 2;
}
echo $this->get_response($id);
}
function Grades () {
$grade = $_POST['grade'];
if ($grade =="1") {
echo "You're a freshmen";
} elseif ($grade == "2") {
echo "You're a sophomore";
} elseif ($grade == "3") {
echo "You're a junior.";
} elseif ($grade == "4") {
echo "You're a senior.";
} else {
echo "Something is wrong.";
}
}
function Gpa () {
$gpa = $_POST['gpa'];
if ($gpa =="1") {
echo "You strongly need to up your GPA.";
} elseif ($gpa == "2") {
echo "You're an average student.";
} elseif ($gpa == "3") {
echo "You're an above average student.";
} elseif ($gpa == "4") {
echo "You're an excellent sudent.";
} else {
echo "Something is wrong.";
}
}
function School () {
$school = $_POST['school'];
if ($school =="1") {
echo "You're into Math";
} elseif ($school == "2") {
echo "You're into Lit";
} elseif ($school == "3") {
echo "You're into history.";
} elseif ($school == "4") {
echo "You're into humanities.";
} elseif ($school == "5") {
echo "You're into science.";
} else {
echo "Something is wrong.";
}
}
And the view page:
<?php $processor = new Processor(); ?>
<h4>Question # 1</h4>
<p><?php Grades($grade); ?></p>
<h4>Question # 2</h4>
<p><?php Gpa($gpa); ?></p>
<h4>Question # 3</h4>
<p><?php School($school); ?></p>
To access the class function use below code,
You have to use it like this $class_variable->function_name()
<?php $processor = new Processor(); ?>
<h4>Question # 1</h4>
<p><?php $processor->Grades($grade); ?></p>
<h4>Question # 2</h4>
<p><?php $processor->Gpa($gpa); ?></p>
<h4>Question # 3</h4>
<p><?php $processor->School($school); ?></p>

Post back the data if validation fails for "Select"

Once submitted selected option, the data is not stored.
just want to know how to post back the data if validation fails
The following line doesnt really work for me.
<select id="numbers" name="numbers" value="<?php echo (isset($_POST['numbers'])) ? $_POST['numbers'] : " "; ?>"/>
if someone could give me a hand?
Many thanks, here is my code
<?php
if(isset($_POST['numbers']) &&($_POST['fruits']) && $_POST['numbers'] != "null" && $_POST['fruits'] !== "null")
{
echo "Thank you!";
} elseif (isset($_POST['numbers']) && $_POST['numbers'] = "null") {
echo "you forgot to choose a number";
}
elseif(isset($_POST['fruits']) && $_POST['fruits'] = "null")
{
echo "you forgot to choose fruit name";
}
?>
<form id="form" name="form" method="post" action="">
<label for="expiry">Select</label>
<select id="numbers" name="numbers" value="<?php echo (isset($_POST['numbers'])) ? $_POST['numbers'] : " "; ?>"/>
<option value="null" selected="selected">-</option>
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
</select>
<select id="fruits" name="fruits" value="<?php echo (isset($_POST['fruits']))? $_POST['fruits'] : ''; ?>"/>
<option value="null" selected="selected">-</option>
<option value="Apple">Apple</option>
<option value="Banana">Banana</option>
<option value="Pear">Pear</option>
</select>
<input type="submit" value="Send" />
</form>
Solved it, Maybe not a best way, but at least got it sorted:
<?php
$item = null; #
$itemyear = null;
if(isset($_POST['numbers'])){
$item = $_POST['numbers'];
}
if(isset($_POST['fruits'])){
$itemyear = $_POST['fruits'];
}
if(isset($item) && isset($itemyear) && $item != "null" && $itemyear !== "null")
{
echo "Thank you!";
} elseif ($item == "null") {
echo "you forgot to choose a number";
}
elseif($itemyear == "null")
{
echo "you forgot to choose fruit name";
}
?>
<form id="form" name="form" method="post" action="">
<label for="expiry">Select</label>
<select id="numbers" name="numbers" />
<option value="null" selected="selected">-</option>
<option value="01" <?php if($item == '01'): echo "selected='selected'"; endif; ?>>01</option>
<option value="02" <?php if($item == '02'): echo "selected='selected'"; endif; ?>>02</option>
<option value="03" <?php if($item == '03'): echo "selected='selected'"; endif; ?>>03</option>
</select>
<select id="fruits" name="fruits" />
<option value="null" selected="selected">-</option>
<option value="Apple"<?php if($itemyear == 'Apple'): echo "selected='selected'"; endif; ?> >Apple</option>
<option value="Banana"<?php if($itemyear == 'Banana'): echo "selected='selected'"; endif; ?>>Banana</option>
<option value="Pear"<?php if($itemyear == 'Pear'): echo "selected='selected'"; endif; ?>>Pear</option>
</select>
<input type="submit" value="Send" />
</form>
<?php
echo $item ."-". $itemyear;
?>
the PHP isset() function returns either true or false (depending on whether the input is.. well... set.
You would want to use this:
value='<?php echo (isset($_POST['numbers'])) ? $_POST['numbers'] : ""; ?>
You can't set a value attribute on a <select>. You have to find the correct <option> and set its selected attribute. Personally, I put a data-default attribute on the <select> and then use JavaScript to loop through the options and find the right one.
Oh now I see.
I don't know what the usual thing to do is but one way is to put all the data as a query string when you redirect the user back. The reason why it doesn't stay in the $_POST global is because it's only kept on the page you post to then it's gone.
So when you redirect the user back
if(validationFailed)
{
header("Location: page.php?data=example");
}
The data can then be retrieved in page.php by using
$data = $_GET['data']; // contains "example"

Categories