What is the quickest and easiest way to run a small php script on my html page?
What is the quickest and easiest way to run a small php script on my html page and what do I need to do to get it running? I'm ask开发者_Python百科ing because I use html and css all the time, but have never done anything in php. I'll be using it to create an email form that doesn't open an email client to send it.
First, your host needs to support PHP. Most do.
Make a basic script like this:
<?php
mail('your@email.com', 'Some Subject', print_r($_POST, true));
?>
Then, build yourself an HTML form that points to this script...
<form action="yourscript.php" method="post">
<input type="text" name="SomeField" />
<input type="submit" name="submit" value="Submit" />
</form>
That's all there is to it. HOWEVER, this is problematic. You will get spam. You need to implement CAPTCHA and such. Otherwise you will get e-mails all the time, even if someone just hits this script with their web browser and no POST data.
Read a tutorial and learn some PHP. It will help you in the long run.
There is also a great form example on tizag.com that will help you understand the components at work here. Basically, you have an HTML form with a few fields (SomeField, submit) and when someone submits this form it will send the data to yourscript.php via the POST method. The PHP script can then read the data in the $_POST
array. PHP has a convenient mail()
function that is great for sending basic e-mail messages. The print_r()
function is used to show everything in an array, such as $_POST.
well, the form itself is html, your form will post ( or GET) to your php script and this will send the email and show output.
To execute the php script you need a webserver that supports php (IIS with the php module, apache with php module etc). Your webserver will host the script and then will execute it and return the output to the browser. Also you need access to an SMTP server in order to send the email. Look at php mail for basic usage, and mostly pear mail for a more complete solution ( including smtp auth).
You need to have php installed and configured properly with your server. Then it's as easy as this:
<?php
echo 'Hello World';
?>
Edit: Also, you may need to use the file extension .php on the page you are trying to run the script on. For example index.php - It may or may not work if the extension is .html
Does that work? If so you are ready to make your script. If not please provide more information about your hosting environment.
The .php file is translated just like a normal HTML page if you don't use the opening tag for PHP (), so you can just have normal HTML, and put the PHP somewhere on the page in tags.
精彩评论