Printable Version of Topic

Click here to view this topic in its original format

HTMLHelp Forums _ Server-side Scripting _ How to include PHP code in an htmlpage

Posted by: Forca May 10 2017, 02:20 AM

Hi

I have a website with a form the user has to complete in order to register. I have a PHP script running server side which sanitises the user input and adds it as a another row in a MySQL table.

I have implemented various checks and balances on the user input. I would like the error messages (due to user input error/mistakes) to be displayed on a webpage with my company logo, heading, etc. I've called this my error page. Instead of creating a separate html page for each possible error message, I would like to write all the error messages into an array in PHP.

The idea is then to scroll through the array (using FOREACH) and gather all array keys with non-zero data (i.e. error messages in them) into one package and write them all out one time to a html page with my company header, logo, etc. I have created the html page, but how do I get the PHP collection of error messages to display on this html error page?

In other words how to write PHP code from a server side script to an existing html page?

Thanks.

Posted by: CharlesEF May 10 2017, 06:27 AM

Your server side script can place the 'errors' in a SESSION variable (array). The error page can check for the existence of the SESSION variable and act on it if found. Most of the time a page must have a '.php' extension to run PHP code, unless you have configured your server differently.

Or, you can pass the 'errors' in the URL as a query string and use $_GET to test for the existence of the query string.

Posted by: Christian J May 10 2017, 07:46 AM

QUOTE(Forca @ May 10 2017, 09:20 AM) *

I have a PHP script running server side which sanitises the user input and adds it as a another row in a MySQL table.
...
how do I get the PHP collection of error messages to display on this html error page?

In other words how to write PHP code from a server side script to an existing html page?

When I've done this I've let the PHP script produce three kinds of pages at the same URL as the script: an initiallly empty form page (which submits to itself); a partially filled in form page, with invalid fields highlighted; and a DB query+confirmation page if the form was filled in correctly.

Posted by: Forca May 11 2017, 06:26 AM

I am not to good with sessions yet (am currently working on it) and so looked a bit deeper into Christian's suggestion. I've thought of using embedded PHP in html as follows:

<html>

html code here will produce the company banner and logo like every other page of my website. It will be linked to the same CSS as the rest of the website to ensure consistant styling.

</html>

<?php>
PHP code to process and sanitise the user input form. Formulate error messages if required.
?>

/* Will then use conditional statements in PHP to produce the requisite html. Only two options - one to produce the html that states all the problems encountered with registration and the other a page that states that registration has been successful. */

<?php conditional statement 1>
Error Messages
?>

<?PHP conditional statement 2>
Registration successful. Redirect to login page.
?>

Posted by: Christian J May 11 2017, 07:12 AM

Don't place the PHP after the </html> end tag, since the PHP will output HTML of its own. Also, an HTTP redirect should be created before any other output is sent to the browser. So the best order IMO would be like this:

CODE
<?php>
PHP code to process and sanitise the user input form.

IF registration successful, create HTTP redirect header to the login page, followed by an exit statement.

ELSE echo HTML for company banner and logo, followed by form error messages.
?>

Posted by: Forca May 11 2017, 07:44 AM

Thanks for that Christian. Makes good sense to me. By the way what does IMO stand for?

Posted by: Christian J May 11 2017, 10:59 AM

QUOTE(Forca @ May 11 2017, 02:44 PM) *

By the way what does IMO stand for?

"In my opinion". smile.gif

Posted by: Forca May 11 2017, 05:50 PM

Oh, that's what it's for!

Posted by: Forca May 12 2017, 01:35 AM

I got stuck! So far have

<html>

<body>
html code to display banner and logo

<?php
Code to sanitise user input
?>

<?php if ($password == $username): ?>
<p> Password cannot be the same as the username. </p>
<?php endif ?>

</body>
</html> I keep getting unexpected end of file error for this last line???





Posted by: Forca May 12 2017, 05:26 AM

Christian, as you can see from my last post above, I was thinking along the lines of displaying the company logo and banner firstup irrespective. Then there are 3 options of text to follow below the banner:

a) User registration successful. I'll provide a button to login.

b) User input error messages provided and there'll be a Registration button.

c) System error notification such as failure to connect to server. Message for user to try again later or contact us if problem persists.

IMHO I thought it more suitable to what my objectives but am currently stuck!

Also, there should be a : after endif.

Posted by: Christian J May 12 2017, 07:05 AM

I think there should be a semicolon after the endif, compare http://php.net/manual/en/control-structures.alternative-syntax.php

QUOTE(Forca @ May 12 2017, 12:26 PM) *

I was thinking along the lines of displaying the company logo and banner firstup irrespective.

That should work if you're not going to redirect to another URL.

QUOTE
Then there are 3 options of text to follow below the banner:

a) User registration successful. I'll provide a button to login.

Why not login the user automatically during registration?

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)