fonecave Posted November 3, 2007 Share Posted November 3, 2007 How do i get the code below to return any messages to the same page rather than the blank php page? <?php //$checkline="checkline"; //unused $name=$_POST["name"]; $address=$_POST["address"]; $password=$_POST["password"]; if(empty($name) || empty($address) || empty($password)) { echo "Please Enter name, email and password"; die; } //darkfreaks email checker if (!preg_match("/^(.+)@[a-zA-Z0-9-]+\.[a-zA-Z0-9.]+$/si", $address)) { echo "Error, email not valid"; die; } $namelen=strlen($name); if ($namelen<5) { echo "Error, name entered is under 5 characters long."; die; } $passwordlen=strlen($password); if ($passwordlen<5) { echo "Error, password entered is under 5 characters long."; die; } $efile=fopen("*****privatefile*****.txt","a+") ; if ($efile) { $inuse = false; while ( !feof($efile) && !$inuse) { $f_name = trim(fgets($efile)); $f_address = trim(fgets($efile)); if($f_name == $name) { echo "user '$name' is already used<br>\n"; $inuse = true; } if($f_address == $address) { echo "email '$address' is already used<br>\n"; $inuse = true; } } if(!$inuse) { fwrite ($efile, $name . "\n"); fwrite ($efile, $address . "\n"); fwrite ($efile, $password. "\n"); echo"Registration Succesful, Thank you $name"; echo"<br>You will be one of the first to recieve all of our amazing offers!<br>" ; echo"You will recieve your username and password soon, Check your inbox.<br>" ; echo"Please click your browsers back button to return to fonecave.<br>" ; } fclose($efile); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/75893-solved-php-return-on-same-page/ Share on other sites More sharing options...
ignace Posted November 3, 2007 Share Posted November 3, 2007 their are quiet some ways to accomplish that you could do: basename?message=blablabla <?php if ($_GET["message"] && $_GET["message"] != "") { echo $_GET["message"]; } // or set a message $message = "blablabla"; if ($message && $message != "") { echo $message; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/75893-solved-php-return-on-same-page/#findComment-384119 Share on other sites More sharing options...
fonecave Posted November 4, 2007 Author Share Posted November 4, 2007 That didnt really help, can u change the code and show me how, i want the messages to be printed on the page the post data came from not a new blank page... Quote Link to comment https://forums.phpfreaks.com/topic/75893-solved-php-return-on-same-page/#findComment-384511 Share on other sites More sharing options...
Northern Flame Posted November 4, 2007 Share Posted November 4, 2007 do you mean you want this to be displayed in the same page that your form is located? then you have to move the script to the same page that the form is located, heres and example: <?php function myForm(){ ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> name:<br> <input type="text" name="name" size="20"><br> Address:<br> <input type="text" name="address" size="20"><br> Password:<br> <input type="password" name="password" size="20"><br> <input type="submit" name="submit" value="Submit"> </form> <?php } if(!isset($_POST['submit'])){ myForm(); exit(); } else{ <?php //$checkline="checkline"; //unused $name=$_POST["name"]; $address=$_POST["address"]; $password=$_POST["password"]; if(empty($name) || empty($address) || empty($password)) { echo "Please Enter name, email and password"; myForm(); die; } //darkfreaks email checker if (!preg_match("/^(.+)@[a-zA-Z0-9-]+\.[a-zA-Z0-9.]+$/si", $address)) { echo "Error, email not valid"; myForm(); die; } $namelen=strlen($name); if ($namelen<5) { echo "Error, name entered is under 5 characters long."; myForm(); die; } $passwordlen=strlen($password); if ($passwordlen<5) { echo "Error, password entered is under 5 characters long."; myForm(); die; } $efile=fopen("*****privatefile*****.txt","a+") ; if ($efile) { $inuse = false; while ( !feof($efile) && !$inuse) { $f_name = trim(fgets($efile)); $f_address = trim(fgets($efile)); if($f_name == $name) { echo "user '$name' is already used \n"; $inuse = true; } if($f_address == $address) { echo "email '$address' is already used \n"; $inuse = true; } myForm(); } if(!$inuse) { fwrite ($efile, $name . "\n"); fwrite ($efile, $address . "\n"); fwrite ($efile, $password. "\n"); echo"Registration Succesful, Thank you $name"; echo" You will be one of the first to recieve all of our amazing offers! " ; echo"You will recieve your username and password soon, Check your inbox. " ; echo"Please click your browsers back button to return to fonecave. " ; } fclose($efile); } ?> } Quote Link to comment https://forums.phpfreaks.com/topic/75893-solved-php-return-on-same-page/#findComment-384627 Share on other sites More sharing options...
fonecave Posted November 5, 2007 Author Share Posted November 5, 2007 that code didnt work it gave me a 404 error (i did change the txt file back) plus it wrote all the script on the page and moved my divs around there is definately a script error there somewhere Quote Link to comment https://forums.phpfreaks.com/topic/75893-solved-php-return-on-same-page/#findComment-385003 Share on other sites More sharing options...
trq Posted November 5, 2007 Share Posted November 5, 2007 How do i get the code below to return any messages to the same page rather than the blank php page? I'm not sure I understand your question. echo prints strings to the current page, what exactly is the problem? Quote Link to comment https://forums.phpfreaks.com/topic/75893-solved-php-return-on-same-page/#findComment-385026 Share on other sites More sharing options...
fonecave Posted November 5, 2007 Author Share Posted November 5, 2007 i want to use the code i posted in a simple name email and password registration form but i dont want to use a standalone php file else like you wrote, the echo statement is printing the messages on the blank php page i want them on the same page the form is on but i cnt even get this simple code to work <?php if (isset ($_POST['submit'])) // if the form was submitted, display their name { echo 'Your name is ' .$_POST['name']; } else // form hasn't been submitted, so display the form { echo '<form method="POST" action="<?php echo $PHP_SELF; ?>" enctype="multipart/form-data"> <input type="text" name="name"> <input type="submit" value="Submit!"> </form>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/75893-solved-php-return-on-same-page/#findComment-385040 Share on other sites More sharing options...
rajivgonsalves Posted November 5, 2007 Share Posted November 5, 2007 not coded the full thing but I'll think you'll get the idea <?php $strMessage = ""; if (isset ($_POST['submit'])) // if the form was submitted, display their name { //$checkline="checkline"; //unused $name=$_POST["name"]; $address=$_POST["address"]; $password=$_POST["password"]; if(empty($name) || empty($address) || empty($password)) { $strMessage .= "Please Enter name, email and password<BR>"; } //darkfreaks email checker if (!preg_match("/^(.+)@[a-zA-Z0-9-]+\.[a-zA-Z0-9.]+$/si", $address)) { $strMessage .= "Error, email not valid"; } $namelen=strlen($name); if ($namelen<5) { $strMessage .= "Error, name entered is under 5 characters long."; } $passwordlen=strlen($password); if ($passwordlen<5) { $strMessage .= "Error, password entered is under 5 characters long."; } if ($strMessage = "") { $efile=fopen("*****privatefile*****.txt","a+") ; if ($efile) { $inuse = false; while ( !feof($efile) && !$inuse) { $f_name = trim(fgets($efile)); $f_address = trim(fgets($efile)); if($f_name == $name) { echo "user '$name' is already used <BR>"; $inuse = true; } if($f_address == $address) { echo "email '$address' is already used <BR>"; $inuse = true; } } if(!$inuse) { fwrite ($efile, $name . "\n"); fwrite ($efile, $address . "\n"); fwrite ($efile, $password. "\n"); echo"Registration Succesful, Thank you $name"; echo "You will be one of the first to recieve all of our amazing offers!" ; echo "You will recieve your username and password soon, Check your inbox." ; echo "Please click your browsers back button to return to fonecave." ; exit; } fclose($efile); } } } echo $strMessage."<BR>" echo '<form method="POST" action="<?php echo $PHP_SELF; ?>" enctype="multipart/form-data"> <input type="text" name="name" value="{$_POST['name']}> <input type="submit" value="Submit!"> </form>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/75893-solved-php-return-on-same-page/#findComment-385057 Share on other sites More sharing options...
fonecave Posted November 5, 2007 Author Share Posted November 5, 2007 ive put that code in now i get this - follow the link http://fonecave.110mb.com/login.html Quote Link to comment https://forums.phpfreaks.com/topic/75893-solved-php-return-on-same-page/#findComment-385060 Share on other sites More sharing options...
rajivgonsalves Posted November 5, 2007 Share Posted November 5, 2007 well try this echo $strMessage."<BR>" echo <<< HEREDOC <form method="POST" action="<?php echo $PHP_SELF; ?>" enctype="multipart/form-data"> <input type="text" name="name" value="{$_POST['name']}> <input type="submit" value="Submit!"> </form> HERDOC; Quote Link to comment https://forums.phpfreaks.com/topic/75893-solved-php-return-on-same-page/#findComment-385062 Share on other sites More sharing options...
fonecave Posted November 5, 2007 Author Share Posted November 5, 2007 i think its because im am putting it all in a code oblect in a HTML page not php page, html cant run php can it? Quote Link to comment https://forums.phpfreaks.com/topic/75893-solved-php-return-on-same-page/#findComment-385069 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.