Radon5K Radon5K - 5 months ago 8
PHP Question

MySQL and PHP - Get a variable from PHP and print it in another PHP page

Ok so what I am trying to do is create a php variable called "text", set it into a specific error message when program runs into a problem, and redirect the user to the login page and ask them to enter their credentials again with the error message printed in the button.

This is createpage.php which is page where it takes the user input. This page also prints the message.

<?php
include('create.php');
// Includes Login Script
?>
<head>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script>
$(function(){
$("#header").load("header.html");
$("#footer").load("footer.html");
});
</script>
<title>Mases Krikorian Website</title>
<link rel="stylesheet" type="text/css" href="index.css">
<link rel="stylesheet" type="text/css" href="login.css">
<style>
h2{
font-family: "Arial Black", Gadget, sans-serif;
}
td{
padding-left: 5px;
padding-right: 5px;
}
</style>
<div id="header"></div>
<div class="bgpic"></div>
</head>
<body>
<div class="container">
<div id="content">
<form method="POST" action="create.php">
<h1>Registration Form</h1>
<div>
<input name="username" placeholder="Username" type="text" id="username">
</div>
<div>
<input name="password" placeholder="Password" type="password" id="password">
</div>
<div>
<input name="email" placeholder="Email" type="text" id="username">
</div>
<div>
<div>
<span><?php echo $text; ?></span>
</div>
<a href="/login.php">Login</a>
<input id="submit" type="submit" name="submit" value="Sign Up">

</div>
</form><!-- form -->
</div>
</div>
</body>
<div id="footer"></div>
</html>


This is my
create.php
page which sets the "text" variable based on their input, and also makes connection with my mysql database.

<?php
session_start();
define('DB_HOST', '<im putting my host here>');
define('DB_NAME', '<im putting my db name here>');
define('DB_USER','<im putting my username here>');
define('DB_PASSWORD','<im putting my password here>');
$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " . mysql_error());
$text= '';

function NewUser()
{
$username= $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$a = (strlen($username))/2;
if($a % 2 != 0){
$a = 2;
}
$salt = ($username[0] + $username[strlen($username) - 1] + $username[$a] + m);
$password= $password.$salt;
$password= md5($password);
$query = "INSERT INTO members (username,email,password) VALUES ('$username','$email','$password')";
$data = mysql_query ($query)or die(mysql_error());

if($data)
{
$text= "Account created.";
header('Location: login.php');
$text= "Account created.";
}
}

function SignUp()
{
if(!empty($_POST['username'])) //checking the 'user' name which is from Sign-Up.html, is it empty or have some text
{
$query = mysql_query("SELECT * FROM members WHERE username = '$_POST[username]' AND password = '$_POST[password]'") or die(mysql_error());
$q = mysql_query("SELECT * FROM members WHERE username = '$_POST[username]'");
if(!$row = mysql_fetch_array($q))
{
newuser();
}
else
{
$text= "Account already exists under that username";
header('Location: createpage.php');
$text= "Account already exists under that username";
}
}
else {
$text= "Account already exists under that username";
header('Location: createpage.php');
$text= "Account already exists under that username";
}
}

if(isset($_POST['submit']))
{
SignUp();
}
?>


Note that the page does the redirecting from the "header" call properly but does NOT set the text. Also note that all connections with the database work properly. Thanks for any help.

Answer

You have to pass the error message with the header redirection itself and on the next page catch it via $_GET and print it .

header("Location: createpage.php?msg=".urlencode($text));

On the createpage.php page where you want to print

<?php echo urldecode($_GET['msg']);?>

Though it is not a good practice of sending texts via URL.

Use SESSION to save your variables across multiple pages . You can take a look of how to set variables using session over here .

http://php.net/manual/en/book.session.php

Comments