How can I try my PHP code to see whether it works?

2019-08-17 01:20发布

问题:

I'm trying to make a basic contact form (using HTML and PHP). I usually host my websites as gh-pages on GitHub. However, the form won't work on GitHub as GitHub doesn't allow PHP because "GitHub Pages is a static site hosting service and doesn't support server-side code such as, PHP, Ruby, or Python."

My question is: how can I try out my code to see if it works?

This is the code. Html:

<!DOCTYPE html>
<html>


<head>
    <title></title>
</head>
<body>

    <form action="send_form_email.php" method="POST">
            <input type="text" name="name" placeholder="Full Name">
            <input type="text" name="mail" placeholder="Your e-mail">
            <input type="text" name="subject" placeholder="Subject">
            <textarea name="message" placeholder="Message"></textarea>
            <button type="submit" name="submit">Send e-mail</button>
        </form>

</body>


</html>

PHP:

if (isset($_POST['submit'])) {
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['mail'];
$message = $_POST['message'];


$mailTo = "example@gmail.com";
$headers = "From: ".$mailFrom;
$txt = "You have received an e-mail from ".$name.".\n\n".$message;

mail($mailTo, $subject, $txt, $headers);
header("Location: index.php?mailsend");
}

PS. I know that I can use free services to make forms (such as http://www.enformed.io or https://formspree.io) but my question is whether there is a way to (even locally) check whether my code would work if I hosted it on a proper domain.

Thank you

回答1:

You can install XAMPP and run your pages locally. This package has PHP, Sendmail and Apache server in it. As well as MySQL if you need it.

XAMPP Installer



回答2:

You need to install a web server and PHP to test it locally. You can install PHP since it comes with a simple web server. If instead you want more power over it you could easily install XAMPP.

If you don't want to install anything, the easiest thing is to use a web hosting where you can upload your code and test it there, but you won't have too much flexibility over it.



回答3:

You can try Wamp service!

Have a look: Wamp



回答4:

On Mac you can use MAMP for local On WINDOWS you can use WAMP for local

If you have a domain and hosting you can upload to the domain and access the PHP directly with a browser to debug.

Browsers output the errors of PHP also you can check your log of your server



回答5:

The easiest way out to your problem is downloading a package. There are a few packages that consist of all the components you will need to run your PHP files.

If you're a Windows user, you can either download XAMPP here https://www.apachefriends.org/download.html

or

WAMP here http://www.wampserver.com/en/

I personally prefer XAMPP over WAMP due to easier user interface. But both of them are equally good for your purpose.

Otherwise, if you're a MAC user, you can go with downloading MAMP https://www.mamp.info/en/downloads/

Installation is pretty straight forward, then you can run the XAMPP/WAMP/MAMP control panel which will start PHP/Apache/mySQL.. for you. You can type in localhost in your browser and if you've installed everything correctly it will show you a related XAMPP/WAMP/MAMP page.

From there onward, you can setup and test your code. If you go with XAMPP/MAMP, you will place your code inside htdocs folder and type in localhost/foldername to access your code. So if you've installed XAMPP in C: drive, you will find htdocs inside your XAMPP folder in C.

For WAMP, you will have to find www folder inside WAMP folder.

Hope that helps.



回答6:

You need to have a local server that can be best provided by WAMP. Try installing it and running your pages through it.