Smokey
Smokey

Reputation: 117

PHP Basics - Echo messages in a redirected page

I have a website, which you press a button and a popup DIV loads up.

On this DIV is a JQuery Validator form which submits to a separate PHP file.

The PHP logins to a database through MySQLi and adds a user. Whilst it does it, at each stage it does an echo message (the idea is that I know what its doing).

This leaves me with a white screen with several lines of information. Its fantastically useful but very ugly from the nice popup div registration.

Is there any way, at the end of the PHP it can redirect to another page assuming there was a blank div in it where the echo information can go, and I can jazz the remaining page up with HTML5 and CSS.

If so how do I get the echo messages into this div?

Thanks

Please see the snippet (which is working) below - but go easy on me as its only been a couple of weeks of learning.

function webmailSignUp($db_connection,$db_con_table) //The function for the website REGISTER FORM
{
    $webmailFullName = $_POST['webmailFullName'];
    $webmailUserName = $_POST['webmailUserName'];
    $webmailExEmail = $_POST['webmailExEmail'];
    $webmailPhone = $_POST['webmailPhone'];
    $webmailDOB = $_POST['webmailDOB'];

    //Check that the fields are not empty
    if (checkBlankFieldsError($webmailFullName,$webmailUserName,$webmailExEmail,$webmailPhone,$webmailDOB) == false)
    {   
        echo "There are no empty Form Input Fields<br>";

        //Connecting to MySQL
        if (mysqli_connect_errno($db_connection))
        {
            echo "Failed to connect to MySQL database:" . mysqli_connect_error();
            echo "<br>";
        }
        else
        {
            echo "Connected to database<br>";

            //Check that there is no existing name in the table
            if (checkInField($webmailUserName,$db_connection,$db_con_table,"userName") == false)
            {   
                //Check DOB Field
                $dob = $webmailDOB; //or use for non-iso: convertDate($webmailDOB);
                echo "DOB is: $dob<br>";

                //Binding and Query to prevent SQL injection
                $query = "INSERT INTO $db_con_table(userFullName,userName,userExEmail,userPhone,userDOB) VALUES(?,?,?,?,?)";                    
                $requery = $db_connection->prepare($query);
                $requery->bind_param("sssss",$webmailFullName,$webmailUserName,$webmailExEmail,$webmailPhone,$dob);     

                if ($requery->execute()) 
                {
                    echo "$webmailUserName has been added to the Webmail Database<br>";
                }
                else 
                {
                    echo "bind failed on $webmailUserName <br>";
                }   

                //Close Database
                $db_connection->close();
                echo "Database is Closed.<br>";

            }
            else{echo "There is already a user registered with this username.  Please try a different one.<br>";}
        }       
    }
    else
    {
        echo "There is 1 or more empty input fields. Please try again.<br>";
    }
}

function checkInField($value,$db_connection,$db_con_table, $db_field)  // Checks a value is not within a database field
{       
    $query = "SELECT $db_field FROM $db_con_table WHERE $db_field='$value'";
    $result = $db_connection->query($query) or die($mysqli->error());

    // GOING THROUGH THE DATA
    if($result->num_rows > 0) 
    {
        while($row = $result->fetch_assoc()) 
        {
            echo "User $value found: '$row[$db_field]' in the table $db_con_table column $db_field<br>";
            return true;
        }
    }
    else
    {
        echo "User $value has not been found in the table $db_con_table column $db_field<br>";
        return false;
    }
}

function checkBlankFieldsError($field1,$field2,$field3,$field4,$field5) //Checks if form fields are blank
{
    $fields = array($field1,$field2,$field3,$field4,$field5);       
    $error = false;

    foreach($fields AS $fieldname)   //Loop trough each fieldname in the fields array
    {
        if(empty($fieldname)) 
        {
            $error = true;
        }
        else 
        {
        }
    }
    return $error;
}

function convertDate($aString) //Converts a String to a Date in Y-M-D format
{
    $date2 = DateTime::createFromFormat('m/d/Y', $aString);
    return $date2->format('Y-m-d');
}

//Main Code Sequence on form buttons
if(isset($_POST['webmailRegisterSubmit']))
{
    webmailSignUp($mysqli_db,$db_table);
    echo "End of Registration.<br>";
}
if(isset($_POST['webamilForgottenPWSubmit']))
{
    webmailForgottenPassword();
    echo "End of Password Reset Request.<br>";
}

Upvotes: 0

Views: 299

Answers (1)

Jscti
Jscti

Reputation: 14440

If you really want a redirection, you will have to store your messages somewhere. I suggest you to save them in the user session. The workflow would be :

  • user do action (save form / get page : anything)
  • server treat the request and store a new "message" in a specific array in the user session (standard php $_SESSION) depending on the situation (error message ? success message ?). At this point you should store the message and its level (INFO/WARNING/ERROR/SUCCESS/etc)
  • server do a rediction (if needed)
  • create a method which :
    • retrieve all store message and delete them directly cause you want to display them only once
    • display them on your DIV
  • you're done

The good thing with this worklow is that it will work even without a redirection as you separate clearly messages addition/storing and display.

Hope this helps

Upvotes: 1

Related Questions