0

Ok this is a basic question. I suspect that I do not know the correct terminology to search for the answer.
I am playing in a development environment and this is all about learning and pushing my skills to learn more.

My main web site is an HTML Form that performs some actions when the Submit button is pressed. No problems there.

I want to write a PHP program that creates and initialises a mysql database. I can do that. This only needs to run one time.

When the PHP program finishes I want it to automatically jump to the HTML document that displays the form. The HTML form is the normal landing page, I am merely saving myself from having to manually load the form or code in an href link to click.
I want to just to go directly there when the database initialisation is complete.

Alternately I could start with the HTML form which branches conditionally depending on whether the database exists. But I still want to return to my original form when that action is complete.

I can do this in high level languages , but I am still understanding the "way" that things are coded for the web.

TIA

3
Contributors
5
Replies
22
Views
3 Years
Discussion Span
Last Post by TheBludger
0

You can have PHP and HTML intermingle, so the form can post to the page that it is resident on, then simply re-render..

<?php
//input.php
if (isset($_POST['formdata']))
{
  //do some input cleaning/error checking/sql stuff here
};
?>
<html>
<head>
<title>My page and form!</title>
</head>
<body>
<form action="input.php" method="post">
<input type="text" name="formdata" /> <!--you can do some error checking stuff with php and have elements display if there is an errror defined in the script... -->
<input type="submit" value="Submit" />
</form>
</body>
</html>
0

OK. Thanks that is half the question answered. I was aware that I can use that type of methodology for the Submit. But if possible I want to keep them seperate. I am still thinking in terms of programming in Cobol and Fortran!

I still want to do the one-off Database Creation and Initialisation. I would prefer to do that in a seperate PHP file and then automatically run a similar file to what you outline above.

Obviously I can do that by having two programs and launch them seperately and consecutively. Eg.
createdatabase.php and
input.php (similar to your suggestion)

It seems obvious to me that you do not want that database code inside input.php. because you don't want your web site users able to initialise databases.

I was just trying to be clever and use createdatabase.php to launch input.php for me. This is pure learning and understanding the right way of programming in PHP/HTML.
Thanks again.

0

The only "good" way of doing what you are saying is to use header redirects.

the database connection will only fire if your logic tells it to. There is nothing wrong with keeping those processes separated, and in general that is good practice (the db connection is created by a function call from an included file that is in a private directory).

however, the "PHP" way of doing things is what I did above. It is by no means the only way, and certainly may not be the right way for tour particular application.

if you are expecting high volume and you are worried about random hits to the db, you will need to code defensively to protect against that. You apparently know your stuff. Go with what you know works :)

0

It seems obvious to me that you do not want that database code inside input.php. because you don't want your web site users able to initialise databases.

Yes, it is good practice to keep them separated. However, PHP code is not readable by your website users, they only see the resulting output.

0

Ok. Thanks.
My Learning here is to forget my ingrained knowledge and do things the web way.

Look out for the next Newbie questions when I start using POST and GET!

Thanks ryantroop for your code snippet by the way. You can be sure that I will add it to my reference library.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.