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.


Recommended Answers

All 5 Replies

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

if (isset($_POST['formdata']))
  //do some input cleaning/error checking/sql stuff here
<title>My page and form!</title>
<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" />

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.

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

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.