Form action

Hi, i have a log in page that leads to the user details in the user details i want to have a text field when the user can send text to the admin user, but i want this text to be showing on the user details page, i am doing the text field with a form action and i want it to return the same page with the new text, but on this page i have a variables which they are the result of the old login form action , and after the text field old the old variables are being undefined, please help i am getting really desperate. my code is:
[php]

Welcome <?php print $_session['email']; ?> to your detials

Home
logout

Your Name is: <?php print $result['name']; ?>

Your Email is: <?php print $result['email']; ?>

Your Password is: <?php print $result['password']; ?>

Your Phone Number is: <?php print $result['phonenumber']; ?>

Your Text is: <?php print $result['text']; ?>


<?php $con=mysql_connect('localhost','root',''); if(mysql_select_db('mydata',$con)) { $query=mysql_query("SELECT * FROM chat WHERE id='$result[id]'",$con); // here i made a loop that showing all the chatting boxes. while($re=mysql_fetch_array($query)) { echo "" . $re['inbox'] . $re['time'] . " " ; echo "
"; } if(isset($_POST['click'])) { echo "" . $_POST['text'] . " " ; }

}

?>


[/php]

To be honest, I’m confused by what you posted.

Are you saying after you click the “Send” button and the form post back you are losing the information in the $result query?

And by the way, you should remove the action="" - It’s not needed, if you’re posting back to the same page and from what I read, it won’t work in HTML 5.

yes exactly , i lose all the $result query is there a way to keep these information, Please help i have to submit my project as fast as possible, thank you very much for your feedback.

Topcoder, is there a way to work around it??

If you’re just doing this for a school project, just put it in a session variable. Like below.

[php]

if(isset($_SESSION[‘result’]) && !empty($_SESSION[‘result’])) {
$result = $_session[‘result’];
} else {
$_session[‘result’] = $result;
}

Welcome <?php print $_session['email']; ?> to your detials

Home
logout

Your Name is: <?php print $result['name']; ?>

Your Email is: <?php print $result['email']; ?>

Your Password is: <?php print $result['password']; ?>

Your Phone Number is: <?php print $result['phonenumber']; ?>

Your Text is: <?php print $result['text']; ?>


<?php $con=mysql_connect('localhost','root',''); if(mysql_select_db('mydata',$con)) { $query=mysql_query("SELECT * FROM chat WHERE id='$result[id]'",$con); // here i made a loop that showing all the chatting boxes. while($re=mysql_fetch_array($query)) { echo "" . $re['inbox'] . $re['time'] . " " ; echo "
"; } if(isset($_POST['click'])) { echo "" . $_POST['text'] . " " ; }

}

?>


[/php]

i still get the same error. i have tried it before but i don’t know what i am doing wrong

My mistake, $_SESSION needs to be in all caps, try the code below.

[php]

if(isset($_SESSION[‘result’]) && !empty($_SESSION[‘result’])) {
$result = $_SESSION[‘result’];
} else {
$_SESSION[‘result’] = $result;
}

Welcome <?php print $_SESSION['email']; ?> to your detials

Home
logout

Your Name is: <?php print $result['name']; ?>

Your Email is: <?php print $result['email']; ?>

Your Password is: <?php print $result['password']; ?>

Your Phone Number is: <?php print $result['phonenumber']; ?>

Your Text is: <?php print $result['text']; ?>


<?php $con=mysql_connect('localhost','root',''); if(mysql_select_db('mydata',$con)) { $query=mysql_query("SELECT * FROM chat WHERE id='$result[id]'",$con); // here i made a loop that showing all the chatting boxes. while($re=mysql_fetch_array($query)) { echo "" . $re['inbox'] . $re['time'] . " " ; echo "
"; } if(isset($_POST['click'])) { echo "" . $_POST['text'] . " " ; }

}

?>


[/php]

I couldn’t understand your code and question. What are you trying to accomplish??? I looked into your code but couldn’t understand it either ;D

[php]

Your Name is: <?php print $result['name']; ?>

Your Email is: <?php print $result['email']; ?>

Your Password is: <?php print $result['password']; ?>

Your Phone Number is: <?php print $result['phonenumber']; ?>

Your Text is: <?php print $result['text']; ?>

[/php]

Where have you declared the $result[] array??? same is true when you are create a SQL query where have you defined $result[id]’ and one thing more you are using a session variable but haven’t started a session in the script.

Please write you question in detail or what you want to accomplish so that we can help you.

Hi Guys and thank you very much for your feedback, basically i have a login page
php

<?php $error=""; $show=TRUE; if(isset($_POST['submit'])) { $username=$_POST['username']; $password=$_POST['password']; if($_SERVER['REQUEST_METHOD']=='POST') { $problem=FALSE; if(empty($username) or empty($password)){$problem=TRUE; $error= '

Invalid password or username

';} else { if(!$problem) $con=mysql_connect('localhost','root',''); mysql_select_db('mydata',$con); session_start(); if ($username && $password == 'admin') { include('admin.php'); goto a; } $sql=mysql_query("select * from users where(email='$username' and password='$password')"); $result= mysql_fetch_array($sql); if($result[0]==0) { $error= '

Invalid account.new user? please click here to register.

'; }else { $show=FALSE; $_session['email']=$username; include('core.php'); } } } } if($show) { ?>
Login page

Login Page


Username:

Password:

<?php print "$error" ?>

<?php } echo "valid account"; a: ?>

from this code and after the user name and the password is been verified i show the client details ,
php

Welcome <?php print $_session['email']; ?> to your detials

Home
logout

Your Name is: <?php print $result['name']; ?>

Your Email is: <?php print $result['email']; ?>

Your Password is: <?php print $result['password']; ?>

Your Phone Number is: <?php print $result['phonenumber']; ?>

Your Text is: <?php print $result['text']; ?>


<?php $con=mysql_connect('localhost','root',''); if(mysql_select_db('mydata',$con)) { if(isset($_SESSION['result']) && !empty($_SESSION['result'])) { $result=$_SESSION['result']; } else { $_SESSION['result']=$result; }

$query=mysql_query(“SELECT * FROM chat WHERE id=’$result[id]’”,$con);
// here i made a loop that showing all the chatting boxes.
while($re=mysql_fetch_array($query))
{
echo “” . $re[‘inbox’] . $re[‘time’] . " " ;
echo “
”;
}
if(isset($_POST[‘click’]))
echo $_POST[‘text’];

}

?>


from here i want the client to be able to send a message to the admin user and i want the messages to appear on the same user details page

Ok. I will reply you back after checking your code. It might take some time. ;D

thank you very much

Ali you have over-complicated your code, keep it simple. I have rewritten the login.php page for you, but I have no knowledge about your “chat” database. You aren’t saving the the shouts in the database but trying to echo it out. Please explain how you are saving information in the chat database???

Here is the code that I’ve rewritten for you.

login.php
[php]

<?php session_start(); require_once ('functions.php'); if (isset($_SESSION['email'])) { header("Location: core.php"); } ?> Login page <?php if( $_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST['username']) && !empty($_POST['password']) && isset($_POST['submit']) ) {
		$conn = db_connect();
		$username = safe_output($_POST['username']);
		$password = safe_output($_POST['password']);

		$result = query_usercheck($conn, $username, $password);
		
		
		if (!is_bool($result)) {

			$username = $result['email'];
			$password = $result['password'];
			$userid = $result['id'];

			if ($password == 'admin') {

				/* I have no idea about these scripts but I included it for your refernce, uncomment it if you want */	

				//include('admin.php');
				// goto a;
			}

			$_SESSION['email'] = $username;
			$_SESSION['id'] = $userid;

			  /* I'm assuming that core.php file is the file where you want users to post message, if thats the case. Just uncomment the header line and delete the include. */

				  header("Location: core.php");
			   //  include('core.php');

		} else {

			echo "
					Username / Password Invalid, Please Try again!
					or if you are a new user please click 
					<a href=\"test.php\"> here</a> to register.</p>
				";
		}
	}

?>

<div id="form">
<form action="" method="POST">
<p>
	<label for='username'>Username: </label>
	<input type="text" name="username" size="30" id="username" />
</p>
<p>
	<label for='password'>Password: </label>
	<input type="password" name="password" size="30" id="password" />
</p>

<p>
	<input type="submit" name="submit" value="submit"/>
</p>
<form/>
</div>
<?php if (isset($conn)) {
 	mysqli_close($conn);

 }

?>

[/php]

functions.php
[php]<?php

function db_connect(){
/* Change the following according to your database connection variables */
$host = ‘localhost’;
$user = ‘tanzeelniazi’;
$pass = ‘abc’;
$db = ‘phphelp’;

$conn = mysqli_connect($host, $user, $pass, $db);

if (mysqli_connect_errno()){

	die("Can't Connect: " . mysqli_connect_erro());

}

return $conn;

}

function safe_output($string){

$string = trim($string);
$string = strip_tags($string);
$string = htmlspecialchars($string);

return $string;

}

function query_usercheck($conn, $username, $password){

$query = "SELECT * FROM users 
				WHERE email = '{$username}' AND
					  password = '{$password}'
		 ";

$results = mysqli_query($conn, $query);

if (!$results) {

	die("Query Failed " . mysqli_error($conn));
}

if (mysqli_affected_rows($conn) == 0){

	return FALSE;
}

$user = mysqli_fetch_assoc($results);

return $user;

}

?>[/php]

please change the code in functions.php from

[php]die("Can’t Connect: " . mysqli_connect_erro());[/php]

to

[php]die("Can’t Connect: " . mysqli_connect_error());[/php]

I made a typo there ;D

Tanzee, thank you for much for your help , is it ok to have your skype, just to ask you some small stuff, i hope you don’t mind my skype is: anthony.aa86 i will be glad to have you as a friend.

I’ll be glad to add you as a friend Ali, but the fact is that I’m not a native English speaker. I can’t speak in English as fluently as I write, therefore, this experience can be intimidating for both of us ;D

Although I will sent you a request during the day, because it’s 01:00 A.M here in Pakistan, so can’t talk right now :stuck_out_tongue: but it would be better if you write your question here.

Sponsor our Newsletter | Privacy Policy | Terms of Service