Jump to content

[SOLVED] php - return on same page


fonecave

Recommended Posts

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);

}

 

 

?>

Link to comment
https://forums.phpfreaks.com/topic/75893-solved-php-return-on-same-page/
Share on other sites

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;
}

?>


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);
}

?>
}

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>';

}

 

?>

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>';

?> 

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;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.