Beginner PHP $_POST issue (input type?) - php

I'm trying to figure out what's going wrong here. I'm trying to post a question number to the page, so as you answer questions it increments up. For some reason I can't get it to post, the only way I got it to work was when I include var_dump($q_num); after the part where I increment the $q_num variable.
I'm assuming this is something very simple but I'm not sure what it is. Any help would be appreciated, thanks!
I know I should sanitize the input, and I've tried a FILTER
<?php
include('inc/quiz.php');
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Math Quiz: Addition</title>
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/styles.css">
</head>
<body>
<?php
if (isset($_POST['page'])){
$q_num = (int)$_POST['page'];
} else {
$q_num = 1;
}
$p_num = 10;
?>
<?php var_dump($q_num);?>
<div class="container">
<div id="quiz-box">
<p class="breadcrumbs">Question #<?= $q_num;?> of <?= $p_num;?></p>
<p class="quiz">What is <?php
?> </p>
<form action="index.php" method="POST">
<input type="hidden" name="page" value="<?php ($q_num+1);?>" />
<input type="submit" class="btn" name="answer" value="135" />
<input type="submit" class="btn" name="answer" value="125" />
<input type="submit" class="btn" name="answer" value="115" />
</form>
</div>
</div>
</body>
</html

Related

MadLibs overwrite inputfields

im trying to make a madlibs kind of game, right now i got everything working besides 1 thing. the output should overwrite the questions instead of showing above the questions. how can i manage to do this?
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Mad Libs</title>
<link rel="stylesheet" type="text/css" href="MadLibs.css">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Libre+Baskerville:wght#700&display=swap" rel="stylesheet">
</head>
<body>
<form action="" method="POST">
<h1> MadLibs</h1>
<?php
if($_SERVER["REQUEST_METHOD"] == "POST"){
$Input1 = $_POST["Input1"];
$Input2 = $_POST["Input2"];
$Input3 = $_POST["Input3"];
$Input4 = $_POST["Input4"];
echo "here is a test $Input1. here is an other test $Input2 and again an other test
$Input4 test $Input3.";
}else{
<p id="Text">test question <input type="text" required name="Input1" placeholder="Enter your answer.."></p>
<p id="Text">test question <input type="text" required name="Input2" placeholder="Enter your answer.."></p>
<p id="Text">test question<input type="text" required name="Input3" placeholder="Enter your answer.."></p>
<p id="Text">test question <input type="text" required name="Input4" placeholder="Enter your answer.."></p>
<input id="Submit" type="submit" name="submit" value="Send">
</form>
?>
<footer>
<p> A #2021 website</p>
</footer>
</body>
</html>
The end result shows like this but the questions and the input field should be gone when the submit button is pressed

500 Error When Attempting to Echo Form data in PHP

I am getting an error page from the server when trying to view my test.php page. Everything on the page loads fine if I remove the php tags.
I am getting an "http error 500" with the following code:
<!doctype HTML>
<html>
<head>
<meta charset="UTF-8">
<Title>Test</Title>
<link rel="stylesheet" type="text/css" href="/CSS/default.css">
</head>
<body>
<?php
echo "<form>
<input type="hidden" name="uid" value="Anonymous">
<input type="hidden" name="date" value='".date()."'>
<textarea name="Message"></textarea><br>
<button type="submit" name="submit">Comment</button>
</form>";
?>
</body>
</html>
Or even strip out most of the echo/print
<!doctype HTML>
<html>
<head>
<meta charset="UTF-8">
<Title>Test</Title>
<link rel="stylesheet" type="text/css" href="/CSS/default.css">
</head>
<body>
<form>
<input type="hidden" name="uid" value="Anonymous">
<input type="hidden" name="date" value="<?php print(date()); ?>">
<textarea name="Message"></textarea><br>
<button type="submit" name="submit">Comment</button>
</form>
</body>
</html>
```
<?php
echo '<form>
<input type="hidden" name="uid" value="Anonymous">
<input type="hidden" name="date" value="'.date().'">
<textarea name="Message"></textarea><br>
<button type="submit" name="submit">Comment</button>
</form>';
?>
```
this should work.
Use the following:-
<!doctype HTML>
<html>
<head>
<meta charset="UTF-8">
<Title>Test</Title>
<link rel="stylesheet" type="text/css" href="/CSS/default.css">
</head>
<body>
<?php
echo "<form>
<input type=\"hidden\" name=\"uid\" value=\"Anonymous\">
<input type=\"hidden\" name=\"date\" value='".date()."'>
<textarea name=\"Message\"></textarea><br>
<button type=\"submit\" name=\"submit\">Comment</button>
</form>";
?>
</body>
</html>

Html/php form not out-putting

hi i'm trying to implement a small feature including a price checker (just testing for now!) but when i hit submit nothing is ecohed back to me! here is my code:
<!doctype html>
<head>
<meta charset="UTF-8">
<title>ValleyRangesPriceCheck</title>
<link REL='stylesheet' TYPE='text/css' HREF='Style.css'>
</head>
<body>
<form>
<input type="date" name="check-in">
<input type="date" name="check-out">
<input type="number" name="num-of-guests" min="1" max="10" placeholder="Guests">
<select name="operator">
<option>Candelight</option>
<option>Misty Woods</option>
<option>Beach Mont</option>
</select>
<button type="submit" name="submit" value="submit">submit</button>
</form>
<p> The Answer Is:</p>
<?php
if (isset($_GET['submit'])){
$result1 = $_GET['check-in'];
$result2 = $_GET['check-out'];
$operator = $_GET['operator'];
switch ($operator){
case "Candelight":
echo $result1 + $result2;
break;
}
}
?>
</body>
</html>
Thanks for the help!
The form method and action attributes are not defined. You should define where the user data should be posted.
Try this code,
<!doctype html>
<head>
<meta charset="UTF-8">
<title>ValleyRangesPriceCheck</title>
<link REL='stylesheet' TYPE='text/css' HREF='Style.css'>
</head>
<body>
<form method="get" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="date" name="check-in">
<input type="date" name="check-out">
<input type="number" name="num-of-guests" min="1" max="10" placeholder="Guests">
<select name="operator">
<option>Candelight</option>
<option>Misty Woods</option>
<option>Beach Mont</option>
</select>
<button type="submit" name="submit" value="submit">submit</button>
</form>
<p> The Answer Is:</p>
<?php
if (isset($_GET['submit'])){
$result1 = $_GET['check-in'];
$result2 = $_GET['check-out'];
$operator = $_GET['operator'];
switch ($operator){
case "Candelight":
echo $result1 + $result2;
break;
}
}
?>
</body>
</html>

Beginner: IF statement/PHP/HTML issue

I do apologise in advance for my lack of knowledge in PHP and HTML. I have scoured the internet for 3/4 days trying to create what is probably a simple system. I need the user to enter a digit before the next page is loaded. I have an IF statement on my page.
1.php
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<link href="css/reset.css" rel="stylesheet" type="text/css" />
<div id="container">
<div id="authorise">
<form action="2.php" method="post">
<!--Product Comment Box--><br>
<p>Please enter your 4<br> digit authorisation code:<br> <br>
<input type="text" name="digits" />
<input type="submit" name="submit" />
</form>
</div>
</div>
2.php
<?php
if ($_POST['digits'] === '210392')
{echo 'https://www.facebook.com/'
?>
But I need a form first which the user would input the code '210392' and press submit. Then the if statement can take place. I know how to do a form but dont know how to assign that form a variable name 'digits'. All help is appreciated.
A basic form:
<form action="your_file.php" method="post">
<input type="text" name="digits" />
<input type="submit" name="submit" />
</form>
Then in your_file.php:
<?php
if ($_POST['digits'] === '210392') {
// etc.
$_POST is a superglobal array that has all data POSTed to your script. digits in this case is an index in that array. It comes from the name of the input field.
<form action="check.php" method="POST">
<input type="text" name="digits" />
<input type="submit" value="click here" />
</form>
//in check.php
if (isset($_POST['digits']) and $_POST['digits'] == 12345){
header("Location: http://google.com");
}
else {
echo " error !!";
}
study html from w3school
http://www.w3schools.com/html/html_forms.asp
and php
http://www.w3schools.com/PhP/php_forms.asp
<form action="https://scm-intranet.tees.ac.uk/users/l1071039/bobbin/admin.php" method="post">
Password : <input type="password" name="pwd" />
<input type="submit" name="send">
</form>
// admin.php
<?php
if( isset($_POST['pwd']) ){
$pwd = $_POST['pwd'];
}
?>
You first need to create an input with the very important name attribute which you'll use later.
<form method="post">
<input type="password" name="mypassword" />
<button type="submit">Go</button>
</form>
PHP:
//Post method access with $_POST variable:
if ($_POST['mypassword'] == 'my-password') { //Oh, you should probably use SSH and hash the password
//Hooray!
}
Instead of echo, use header.
<?php
if ($_POST['digits'] === '210392')
{
header (loaction:https://www.facebook.com/);
}
?>

php/jquery-mobile: Second page not running validation on two page form

I'm new to php and am trying to make a very simple two page form. Any help or guidance would be very appreciated!
Problem:
The second page does not run error validation when coming to it from the first page.
Both pages run error validation correctly URL's entered directly
Setup:
Page 1 one is HTML. It POSTS to Page2.
Page 2 is php and the data from Page 1 is stored in an input field
Live example:
http://www.linplusg.com/page1.html
In IE the Page 2 URL after coming from Page 1 looks to be incorrect:
http://www.linplusg.com/page1.html#/page2.php
In FF and Chrome the URL looks fine but I think there's a flicker/refresh happening.
Does something else happen besides just opening the page when doing a POST to a page? Does something value get stored the new fields? Am I doing the form action wrong? Dazed and confused...
Thank you so much for any help or suggestions. I've been searching around for answers all night and my brain feels like jello. It seems like I'm missing something simple?!
Page1 Code
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>
</title>
<link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<link rel="stylesheet" href="css/my.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
</script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.js">
</script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.10.0/jquery.validate.min.js">
</script>
<script>
$(document).ready(function(){
$("#initialForm").validate();
});
</script>
</head>
<body>
<!-- Home -->
<div data-role="page" id="page1">
<div data-role="content">
<form id="initialForm" method="post" action="page2.php">
<div data-role="fieldcontain">
<fieldset data-role="controlgroup" data-mini="true">
<label for="textinput3">
Zip Code
</label>
<input name="zip" id="zip" maxlength=5 value="" type="text" class="required" />
</fieldset>
</div>
<input type="submit" data-theme="e" value="Submit" />
</form>
</div>
</div>
</body>
</html>
Page 2 code
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>
</title>
<link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<link rel="stylesheet" href="css/my.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
</script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.js">
</script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.10.0/jquery.validate.min.js">
</script>
<script>
$(document).ready(function(){
$("#fullForm").validate();
});
</script>
</head>
<body>
<!-- Home -->
<div data-role="page" id="page1">
<div data-role="content">
<form id="fullForm" method="get" action="">
<div data-role="fieldcontain">
<fieldset data-role="controlgroup" data-mini="true">
<input name="zip" id="zip" value="<?php echo $_POST["zip"]; ?>" type="" />
</fieldset>
</div>
<div data-role="fieldcontain">
<fieldset data-role="controlgroup" data-mini="true">
<label for="textinput3">
First Name
</label>
<input name="first_name" id="first_name" placeholder="" value="" type="text" class="required"/>
</fieldset>
</div>
<input type="submit" data-theme="e" value="Submit" />
</form>
</div>
</div>
</body>
</html>
To prevent this you need to specify data-ajax="false" in the form element in page1.
<form id="initialForm" method="post" action="page2.php" data-ajax="false">
see http://jquerymobile.com/test/docs/forms/forms-sample.html for more information.
Without the PHP code, I can just guess, but maybe it is because in the second form you have a method="get" instead of method="post", and in PHP you check the $_POST variable?
You could preserve the ajax transitions and still have your javascript execute on your second page by moving your script inside the div with the data-role "page".
See this answer here: https://stackoverflow.com/a/6428127/2066267

Categories