The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> How to include PHP code in an htmlpage
Forca
post May 10 2017, 02:20 AM
Post #1


Member
***

Group: Members
Posts: 33
Joined: 29-April 16
Member No.: 24,203



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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post May 10 2017, 06:27 AM
Post #2


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



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.

This post has been edited by CharlesEF: May 10 2017, 06:31 AM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post May 10 2017, 07:46 AM
Post #3


.
********

Group: WDG Moderators
Posts: 9,628
Joined: 10-August 06
Member No.: 7



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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Forca
post May 11 2017, 06:26 AM
Post #4


Member
***

Group: Members
Posts: 33
Joined: 29-April 16
Member No.: 24,203



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.
?>
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post May 11 2017, 07:12 AM
Post #5


.
********

Group: WDG Moderators
Posts: 9,628
Joined: 10-August 06
Member No.: 7



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.
?>
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Forca
post May 11 2017, 07:44 AM
Post #6


Member
***

Group: Members
Posts: 33
Joined: 29-April 16
Member No.: 24,203



Thanks for that Christian. Makes good sense to me. By the way what does IMO stand for?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post May 11 2017, 10:59 AM
Post #7


.
********

Group: WDG Moderators
Posts: 9,628
Joined: 10-August 06
Member No.: 7



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

By the way what does IMO stand for?

"In my opinion". smile.gif
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Forca
post May 11 2017, 05:50 PM
Post #8


Member
***

Group: Members
Posts: 33
Joined: 29-April 16
Member No.: 24,203



Oh, that's what it's for!
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Forca
post May 12 2017, 01:35 AM
Post #9


Member
***

Group: Members
Posts: 33
Joined: 29-April 16
Member No.: 24,203



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???




User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Forca
post May 12 2017, 05:26 AM
Post #10


Member
***

Group: Members
Posts: 33
Joined: 29-April 16
Member No.: 24,203



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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post May 12 2017, 07:05 AM
Post #11


.
********

Group: WDG Moderators
Posts: 9,628
Joined: 10-August 06
Member No.: 7



I think there should be a semicolon after the endif, compare http://php.net/manual/en/control-structure...tive-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?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 18th March 2024 - 10:32 PM