if statement php not working properly - php

I have an if else statement. The code logic is that the first if statement would get all the necessary informations needed and the second if statement would print all the informations that are generated at the first if statement. The problem is that when it triggers the second if statement, it disregards all the data that are stored in the first if statement.
Can anybody help me how to solve this problem? Thank You
This is just a sample code but the process and logic of code is somehow the same.
<head>
<title>Sample PHP Web</title>
</head>
<body>
<form method = "post">
<input type = "submit" value = "submit" name = "submit">
<?php
if(isset($_POST['submit']))
{
$nn[0] = "man";
$nn[1]= "men";
echo'<input type = "submit" value = "print" name = "print">';
}
?>
</form>
<?php
if(isset($_POST['print']))
{
echo $nn[0];
echo $nn[1];
}
?>
</body>

After the initial form submission is handled the values you set into PHP variables are lost. They do not persist across page requests. If you want them to persist you need to use sessions.
<?php
session_start();
?>
<head>
<title>Sample PHP Web</title>
</head>
<body>
<form method = "post">
<input type = "submit" value = "submit" name = "submit">
<?php
if(isset($_POST['submit']))
{
$_SESSION['nn'][0] = "man";
$_SESSION['nn'][1]= "men";
echo'<input type = "submit" value = "print" name = "print">';
}
?>
</form>
<?php
if(isset($_POST['print']))
{
echo $_SESSION['nn'][0];
echo " "; // seperate words with a space
echo $_SESSION['nn'][1];
}
?>
</body>

Related

PHP Parse Error: script can not get data from form

I tried to collect data from this form,as a quiz:
<form action="quiz.php" method = "post" id = "questions" name = "quiz">
<label>This is a quiz:</label><br>
<input type="radio" name = "vote1" value = "Yes"/><label>Yes</label>
<input type="radio" name = "vote1" value = "No"/><label>No</label>
<button type = "submit" name="send">Submit</button>
</form>
And I sent it to this php script:
<html>
<?php
if (isset($_POST['send'])){
if (isset($_POST["quiz"])){
if (isset($_POST['vote1'])){
if ($_POST['vote1'] === "Yes"){
echo "Yes,that is the answer.";
}else{
echo "Yes,that is the answer";
};
}};};
?>
<body>
</body>
</html>
But it didn't generate any HTML...
Although you can see in the url,that it definetely tried to pick up data:
http://localhost/php/quiz.php?vote1=Yes&send=
But it didn't really send data and I don't know what to do in that case,can anyone help me?...
It was not displaying output because of if (isset($_POST["quiz"]))
Below code of yours will work, and will display you echo result.
<html>
<?php
if (isset($_POST['send'])) {
if (isset($_POST['vote1'])) {
if ($_POST['vote1'] == "Yes") {
echo "Yes,that is the answer.";
} else {
echo "Yes,that is the answer";
};
}
};
?>
<body>
</body>
</html>
If the form method was GET you would get a querystring as described but the above form uses POST so that would not happen. The only form element of interest in the POST array really is vote1 so ensure that is present using isset - the other items such as the button or the form name ( ? which won't be there, ever! ) are not relevant.
A label element should be associated with a form element in one of two ways: either using the for=ID_of_element type syntax or by having the element as a child, as below.
<html>
<head>
<title>Quiz></title>
</head>
<body>
<form action="quiz.php" method="post" id="questions" name="quiz">
<h1>This is a quiz:</h1>
<label><input type="radio" name="vote1" value="Yes"/>Yes</label>
<label><input type="radio" name="vote1" value="No"/>No</label>
<button type = "submit" name="send">Submit</button>
</form>
<?php
if( isset( $_POST['vote1'] ) ){
$msg='';
switch( strtolower( $_POST['vote1'] ) ){
case 'yes':$msg='Yes,that is the answer.';break;
case 'no':$msg='Sorry, that is not correct';break;
}
echo $msg;
}
?>
</body>
</html>

Not able to place $_SESSION['user'] value (saved in page1) in an input field inside page 2

Don't know were is the mistake lies in the code.
// Code in Page1
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>Test1</title>
</head>
<body>
<FORM METHOD = "POST" action = "Page2.php">
<input type="text" name = "user">
<input type="submit" name = "submit" value = "Submit">
</FORM>
<?php
if (isset($_POST["submit"])){
$_SESSION['user'] = $_POST['user'];
}
?>
</body>
</html>
// Code in Page2
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>Test2</title>
</head>
<body>
<input type="text" name = "field3" value = <?php echo htmlspecialchars($_SESSION['user']); ?>>
</body>
</html>
I expect the input field-named as "field3" in Page2 to be filled with whatever value provided in field-named as "name" of Page1.
You are posting data to page2 when submitting the form
<FORM METHOD = "POST" action = "Page2.php">
The code which is inside the if statement never executed if (isset($_POST["submit"])){
You have to place if statement on Page2 at the top after session_start to make it work
if (isset($_POST["Submit"])){
$_SESSION['user'] = $_POST['user'];
}
The action page is page2 so you will never have a post action on the page 1 where you have html form. In case of you have POST action that leads to page 1 you still have issues on page2.
and On page2, change it as
value =" <?php echo htmlspecialchars($_SESSION['user']); ?>">
You are missing "

Php code is not saving user details to sql database

The php code below seems to run but not save into the database. Its simple yet critical enough to be a bother.
<html>
<body>
<?php
if(isset($_POST['submit'])){
$connect=mysql_connect("localhost","root","root","form2");
if(mysql_connect_error($connect))
{
echo 'Failed to cnnect';
}
$fname=$_POST['fname'];
if(mysql_query("INSERT INTO user(fname) VALUES('$fname')"))
{
echo ' Success';
}else
{
echo ' No good!';
}
}
?>
<form method ="POST">
First name <input type = "text" name = "fname"></input>
<input type = "submit" name = "submit" value ="enter"></input>
</form>
</body>
</html>
When should one use mysqli for security reasons (since mysql is deprecated) and how should it be used?

Comment Section in PHP using HTML form and MySQL database

I'm very new to PHP- and am trying to teach myself a bit of web programming with hands-on learning. I've stumbled upon this: https://www.youtube.com/watch?v=CS45YAqCgX8 video series on how to build a comment section, but am stuck.
My First PHP file looks as such:
<?php
require('connect.php');
If(isset($_POST['submit']))
{
$name=$_POST['name'];
$comment=$_POST['comment'];
If(name&&comment)
{
$insert=mysqli_query("INSERT INTO comment (name,comment) VALUES ('$name','$comment')");
}
}
/*else
echo "Please fill out your name and leave a comment.*/
?>
<html>
<br/>
<br/>
<body>
<form action = "CommentForm.php" method = "post">
<table>
<br/>
<tr><td>Name: </td><td><input type = "text" name = "name" size = "30"/></td></tr>
<tr><td colspan="2">Comment:</td></tr>
<tr><td colspan="2"><textarea name = "comment"></textarea></td></tr>
<tr><td colspan="2"><input type = "submit" name = "submit" value = "Comment"/></td></tr>
</table>
</form>
</body>
</html>
The connection file is:
<?php
mysqli_connect("My Host", "My Username","My Password");
mysqli_select_db("My Database");
?>
-and the HTML I want to attach it to is as such
<!DOCTYPE html>
<html>
<head>
<meta charset = "utf-8">
<title>Pokemon</title>
<link rel = "stylesheet" type = "text/css"
href = "stylishandviewtiful.css">
</head>
<body>
<br/>
<br/>
<br/>
<br/>
<h3>Competitive Meta and Community</h3>
<h3>In-Game Story Ideas and Opinions</h3>
</body>
<?php include 'CommentForm.php'; ?>
</html>
I'll suppose I can't just use an include function? Would I see a better return to separate the HTML in the first PHP application into its own file?
I may generally need a tutorial for this specific instance that starts slow enough for any layman to follow.
You forgot to put the connection inside the mysqli_query($con,"insert ...")
otherwise your insert will be linked to $link. second try making connection like this $con=mysqli_connect("localhost","my_user","my_password","my_db"); pass your $con to your mysqli_query
see http://www.w3schools.com/php/func_mysqli_query.asp
and if your form still does not appear you need to look at your include file. If there is something wrong it will abort

Retain textbox value

My Code:
<html>
<head>
<title>A BASIC HTML FORM</title>
<?PHP
if (isset($_POST['Submit1'])) {
$username = $_POST['username'];
}
?>
</head>
<body>
<Form name ="form1" Method ="POST" Action ="basicForm.php">
<Input Type = "text" Value ="<?PHP print $username ;?>" Name ="username">
<Input Type = "Submit" Name = "Submit1" Value = "Login">
</FORM>
</body>
</html>
Problem: The PHP code gets displayed in the textbox instead of the actual value
That syntax is correct (albeit vulnerable to XSS).
If it doesn't work, then you aren't using a server that supports PHP for the file you are loading.
PHP, on the WWW, only works when the page is accessed:
Through a web server
That has PHP installed
That knows that the file you are loading contains PHP and should be processed by the PHP engine (this is usually done by giving it a .php file extension).
Update: You have edited your question to include a screen grab. This confirms this answer. You are trying to load the PHP from your file system directly into your browser (with a file:/// URI). You need to access it through a web server (with an http:// URI).
As #CORRUPT points out in a comment. You don't give $username a default value, so you will get an error if the POST data isn't set. I suggest setting it to an empty string before the if (isset($_POST['Submit1'])) { line.
Try Below code:
<html>
<head>
<title>A BASIC HTML FORM</title>
<?PHP
if (isset($_POST['Submit1'])) {
$username = $_POST['username'];
}else{
$username='';
}
?>
</head>
<body>
<Form name ="form1" Method ="POST" Action ="basicForm.php">
<Input Type = "text" Value ="<?PHP print $username ;?>" Name ="username">
<Input Type = "Submit" Name = "Submit1" Value = "Login">
</FORM>
</body>
</html>
Write this function in the php block:
function prepopulate($name) {
if(isset($_POST[$name])) {
return $_POST[$name];
}
else {
return "";
}
}
and then in the HTML part
<FORM NAME ="form1" METHOD ="POST" ACTION = "">
<input name="fname" type="text" value="<?php echo prepopulate('fname');?>">
<input name="lname" type="text" value="<?php echo prepopulate('lname');?>">
</FORM>
You just have to pass the name of the textbox to the function prepopulate to retain it after form submit.

Categories