php calculation keeps getting the answer as 0 - php

this is the index.php file
if(isset($_POST['submit']))
{
echo header('Location:answer.php');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="container">
<form method="post">
<div class="form-group">
<label> Insert P value </label>
<input type="text" name="p" placeholder="please insert your P value">
</div>
<br>
<div class="form-group">
<label> Insert R value </label>
<input type="text" name="r" placeholder ="please insert your R value ">
</div>
<br>
<div class="form-group">
<label> Insert your N value </label>
<input type="text" name="n" placeholder=" please insert your N value">
</div>
<br>
<button type="submit" name="submit" > Submit </button>
</body>
</html>
this is the answer.php file
<?php
$p=isset($_POST['p'])?$_POST['p']: " ";
$r=isset($_POST['r'])?$_POST['r']:" ";
$n=isset($_POST['n'])?$_POST['n']: " ";
$si=(int)$p*(int)$r*(int)$n/100;
echo "Hello this is the final answer ".$si;
?>
i want the answer to be displayed in another page that is answer.php but whatever number i am inserting i keep getting the answer as 0. Please help and thank you.

You need to make your form post directly to answer.php. By making it post to itself and then redirecting you're losing all the submitted data - it's sent to your index page and then not transmitted again to the answer page. The redirect causes a separate GET request and doesn't contain any of the submitted data.
The other alternative is to move all the logic into index.php and not bother with a separate script at all.

From what I saw from your code. Inside the form tag, you need to add action='answer.php', this is a direct link to the page you are requesting data from, in this case answer.php.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="container">
<form method="post" action="answer.php">
<div class="form-group">
<label> Insert P value </label>
<input type="text" name="p" placeholder="please insert your P value">
</div>
<div class="form-group">
<label> Insert R value </label>
<input type="text" name="r" placeholder ="please insert your R value ">
</div>
<br>
<div class="form-group">
<label> Insert your N value </label>
<input type="text" name="n" placeholder=" please insert your N value">
</div>
<button type="submit" name="submit" > Submit </button>
</body>
</html>

Related

A problem with processing data from html form with PHP

I'm just about to learn php and that is my first try to process data from an html form. I was writing this php code using some articles from the Internet and it seems to be correct but after entering all the data and than pressing "submit" nothing is happening. I mean just a white browser screen.
Here is my code:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>form</title>
</head>
<body>
<form method="post" action="form__2.php">
<label for="surname">Surname</label>
<input type="text" name="surname" placeholder="Johnson" required> <br>
<label for="name">Name</label>
<input type="text" name="name" placeholder="Carl" required> <br>
<label>Gender:</label>
<label for="male">M</label>
<input type="radio" name="male" checked>
<label for="female">F</label>
<input type="radio" name="female"> <br>
<label for="education">Education:</label>
<select id="education" name="education" required>
<option value="less-basic">Less than basic</option>
<option value="basic">Basic</option>
<option value="mid-school">Middle-school</option>
<option value="high-school">High-school</option>
<option value="bachelor">Bachelor's or equivalent</option>
<option value="master">Master's or equivalent</option>
<option value="doctor">Doctorate or equivalent</option>
</select> <br>
<label for="courses">Enroll in courses</label>
<input type="checkbox" name="courses" required> <br>
<label>Was you dealing with our company later?:</label>
<label>Yes</label>
<input type="radio" name="company" value="yes" checked>
<label>No</label>
<input type="radio" name="company" value="no"> <br>
<input type="submit" name="submit">
</form>
</body>
</html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>form</title>
</head>
<body>
<?
if ( count($_GET) > 0 ) // checking if the form sends any data
{
$name = htmlspecialchars($_GET['name']); // getting name from GET-values
$surname = htmlspecialchars($_GET['surname']); // getting surname from GET-values
if ( strlen($_GET['name']) >= 1 && strlen($_GET['surname']) >= 1) // if the name/surname has more than 1 letter, then..
{
echo "Dear " . $surname . " " . $name . ". We are great to have you signed for our courses. We are hoping for "; // msg
if ( $_POST['company_yes']=='yes' ) // if radiobutton is checked
{
echo "continuing our partnership."; // msg
} else
{
echo "our future partnership."; // msg
}
} else
{
echo "Your name is too short";
}
}
?>
</body>
</html>
You didn't check if a Post happened, and why call to GET request if you send a post request?
A GET request is data that passes through the URL -> https://www.something.net?data=hi
$_GET['data'] will give "hi" value.
POST is data that is passed mostly through forms with method="POST"
To get the data of the form change the form__2.php to the following:
<?php
if(isset($_POST['submit']){ //the name of the submit button
echo $_POST['education'];
echo $_POST['female'];
}
?>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>form</title>
</head>
<body>
</body>
</html>

How can i make it so that the server creates a new html page every time i post a blog on my site?

So i am trying to make the server create a new HTML file every time i create a blog on my page on the make page of my website.
So what i tried is getting all the details with $_POST with this:
HTML:
<form name="bloginput" action="blogpost.php" method="POST" class="bloginputform">
<input type="text" name="bloginput" placeholder="Type your blog here..." id="bloginput" class="bloginput">
<div class="popup">
<input type="button" placeholder="Submit" id="popupbtn" onclick="toggleHideShow()">
<span class="popuptext hide" id="myPopup">
<input type="name" name="author" placeholder="Name:" id="nameinput" class="nameinput">
<br>
<input type="email" name="authoremail" id="emailinput" placeholder="E-mail..." class="emailinput">
<br>
<label class="filelabel" id="filelabel">
<input type="file" name="post-image" accept="image/*" id="fileinput" class="fileinput">
<p class="fileinputtext" id="fileinputtext">
Upload Image:
</p>
</label>
<p><button class="crossbtn" onclick="toggleHideShow()" id="crossbtn">✕</button><input type="submit" value="Post" placeholder="Post" class="submitbtn" id="submitbtn"></p>
</form>
PHP:
<?php
$blogtext = $_POST["bloginput"];
$author = $_POST["author"];
$email = $_POST["authoremail"];
$image = $_POST["post-image"];
$Title = "My blog."
$file = "blogposttest.html"
$code = '
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Blog</title>
<link rel="stylesheet" href="CSS/Blogs.css">
</head>
<body>
<div class="wrapperdiv" id="wrapperdiv">
<h1 class="Title" id="Title">$Title<br>By: $author</h1>
<br>
<br>
<br>
<center>
<img src="$image" name="Blogimage" class="blog-img" id="blog-img">
</center>
<br>
<br>
<br>
<p class="blogtext" id="blogtext">$blogtext</p>
</div>
</body>
</html>
';
file_put_contents($file, $code)
header("Location: http://blogmedia.nl")
?>
I would really like some help on this.
You have some syntax errors in your code. you can use the below code and when I test it will create an HTML file but consider that if you want to create multiple files you should give a dynamic name and also it's better to validate your variables in PHP.
<form name="bloginput" action="#" method="POST" class="bloginputform">
<input type="text" name="bloginput" placeholder="Type your blog here..." id="bloginput" class="bloginput">
<div class="popup">
<input type="button" placeholder="Submit" id="popupbtn" onclick="toggleHideShow()">
<span class="popuptext hide" id="myPopup">
<input type="name" name="author" placeholder="Name:" id="nameinput" class="nameinput">
<br>
<input type="email" name="authoremail" id="emailinput" placeholder="E-mail..." class="emailinput">
<br>
<label class="filelabel" id="filelabel">
<input type="file" name="post-image" accept="image/*" id="fileinput" class="fileinput">
<p class="fileinputtext" id="fileinputtext">
Upload Image:
</p>
</label>
<p><button class="crossbtn" onclick="toggleHideShow()" id="crossbtn">✕</button><input type="submit" value="Post" placeholder="Post" class="submitbtn" id="submitbtn"></p>
</form>
<?php
$blogtext = $_POST["bloginput"];
$author = $_POST["author"];
$email = $_POST["authoremail"];
$image = $_POST["post-image"];
$Title = "My blog.";
$file = "blogposttest.html";
$code = <<<EOD
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Blog</title>
<link rel="stylesheet" href="CSS/Blogs.css">
</head>
<body>
<div class="wrapperdiv" id="wrapperdiv">
<h1 class="Title" id="Title">$Title<br>By: $author</h1>
<br>
<br>
<br>
<center>
<img src="$image" name="Blogimage" class="blog-img" id="blog-img">
</center>
<br>
<br>
<br>
<p class="blogtext" id="blogtext">$blogtext</p>
</div>
</body>
</html>
EOD;
file_put_contents($file, $code)
?>
Use the below condition to check $_POST is empty or not. This resolves as true even if all $_POST values are empty string
if (!empty($_POST))
{
// Your code
}
Check specific key is available in post data. You can use this condition:
if (isset($_POST['author']) )
{
}
Using this code you can check before creating a new file to avoid rewriting an old HTML file.
If the file with the same name exists:
if (file_exists( $file )) {
// todo acction. for example create a random name for your new file.
}

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

PHP session clearing out on page refresh - show and hide based on input

So I have two pages, both pages have an input field, and if the user fills out the input field and goes to the next page they will see new content in the form's place. So it's basically like "fill out this form and show new content". Whether you fill out the form on page A or page B, the other page will recognize if you have filled it out or not. Make sense?
I have it working as long as you don't refresh the page or get to the other page other than clicking the submit input.
What am I doing wrong here?
<!--- Page A -->
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui" name="viewport">
<meta content="noindex, nofollow" name="robots">
<title>Page A</title>
<meta content="Page" name="description">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<form action="/b.php" id="l-form" method="post" name="l-form">
<div class="wrapper localize" style="<?php if(isset($_POST['treasure'])) { echo "display:none;"; } else { echo "display:block;"; } ?>">
<div class="header_container">
<h2>Make sure we offer service at your location</h2>
</div>
<div class="input_container">
<input autocomplete="off" class="input-box street" id="street" name="userLogin" placeholder="Street address" tabindex="0" type="text">
<input autocomplete="off" class="input-box suite" id="suite" name="suite" placeholder="Suite (Optional)" tabindex="0" type="text">
<input autocomplete="off" class="input-box zipcode" id="zipcode" name="zipcode" placeholder="Zip code" tabindex="0" type="text">
<input type="submit" name="treasure" value="See offers">
</div>
</div>
<!-- results 3 -->
<div class="results" style="<?php if(isset($_POST['treasure'])) { echo "display:block;"; } else { echo "display:none;"; } ?>">
<p class="results-heading">Excellent news. We know who you are.</p
</div>
</form>
</body>
</html>
<!--- Page B -->
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui" name="viewport">
<meta content="noindex, nofollow" name="robots">
<title>Page A</title>
<meta content="Page" name="description">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<form action="/a.php" id="l-form" method="post" name="l-form">
<div class="wrapper localize" style="<?php if(isset($_POST['treasure'])) { echo "display:none;"; } else { echo "display:block;"; } ?>">
<div class="header_container">
<h2>Make sure we offer service at your location</h2>
</div>
<div class="input_container">
<input autocomplete="off" class="input-box street" id="street" name="userLogin" placeholder="Street address" tabindex="0" type="text">
<input autocomplete="off" class="input-box suite" id="suite" name="suite" placeholder="Suite (Optional)" tabindex="0" type="text">
<input autocomplete="off" class="input-box zipcode" id="zipcode" name="zipcode" placeholder="Zip code" tabindex="0" type="text">
<input type="submit" name="treasure" value="See offers">
</div>
</div>
<!-- results 3 -->
<div class="results" style="<?php if(isset($_POST['treasure'])) { echo "display:block;"; } else { echo "display:none;"; } ?>">
<p class="results-heading">Excellent news. We know who you are.</p
</div>
</form>
</body>
</html>
You're not using session variables anywhere. When someone submits your form, you need to do something like:
$_SESSION['treasure'] = $_POST['treasure'];
to set the 'treasure' session variable. On the other page or after a refresh, you can then access $_SESSION['treasure'].
Currently, this seems to work when you click submt, because you are making $_POST['treasure'] available to the target page, not because you are using sessions as intended.

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