PHP scripts not running on my website - php

I am currently going through elithecomputerguy's youtube playlist for php programming. I am on part 7 of his 11 part series, this section talks about sending email with php. He writes out all the php code on notepad++ and uploads it to a standard godaddy web hosting account. I am using the same exact method, copying his code line by line, setting up the files in the same location and when I upload my email_form.php, it displays correctly
<HTML>
<HEAD>
</HEAD>
<BODY>
<form action="email_script.php" method="POST">
<p>Email Address: <input type="text" name="email" size="30"></p>
<p>Subject: <input type="text" name="subject" size="30"></p>
<p>Message: </p>
<p><textarea rows="10" cols="20" name="message"></textarea></p>
<input type="submit" name="submit" value="Submit">
</BODY>
</HTML>
Now I uploaded my email_script.php file just fine and it looks like so:
<?PHP
$from="test#mikesmtgadvice.com";
$email=$_POST['email'];
$subject=$_POST['subject'];
$message=$_POST['message'];
mail($email, $subject, $message,"From:".$from);
print "Your message has been sent: </br?$email</br>$subject</br>$message</p>
?>
so when I go to mysite.com/test/email_form.php, it works and displays this form the way it should. However, when I fill in the form and hit submit, when it tries running the script, I get an 500 Internal Server Error. I have no idea where to look for this error and cannot find the error log, and therefore cannot figure this out. In the video, he does this exact same process and it works, is there a setting I need to change in my server or?
Thank you for your help in advance, I'm sorry for the long winded version but I wanted to be as specific as possible.

You are missing a double quote and ; at the end of the print statement:
print "Your message has been sent: <br />$email<br />$subject<br />$message</p>";
Replace that with your print line and it will fix it.

Related

PHP Email Form Issue: Cannot POST

I've been working on getting a PHP email form set up on my personal webpage, and I am running into an issue where when I hit submit, the page displays "Cannot post /mail.php".
I've seen several similar questions on this site before, but I haven't found any approaches that have solved the issue. I am new to PHP so I'm not sure how deep the error could be, or if I'm possibly missing something obvious.
I'm using Brackets Live Preview on Mac OS Big Sur.
Things I've tried:
Made sure that the mail.php file is in the same directory as my HTML files
Made sure I'm setting the name attribute in my HTML file so the PHP can pick up on the relevant entered information
Switched POST with GET (POST errors, GET download the PHP file instead of running it)
Downloaded PHP with Homebrew
Redownloaded Brackets
For reference this is my code:
HTML File:
<form action="mail.php" method="post">
<p>Name</p> <input type="text" name="name">
<p>Email</p> <input type="text" name="email">
<p>Message</p><textarea name="message" rows="6" cols="40"></textarea><br>
<input type="submit" value="Send"><input type="reset" value="Clear">
</form>
PHP File:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From $name \n Message: $message";
$recipient = "email.redacted#gmail.com";
$subject = "Contact Form";
$mailheader = "From $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error");
?>
Any insight into what may be going wrong or some debugging steps to take would be much appreciated!
Here <? php you have whitespace after the ?. Remove it, and see what happens.
It should be <?php to be valid syntax. Otherwise the script breaks.
Edit: I'm not claiming this will solve any logical errors in your code, but that space in there is making the script not even PHP.
Is your form you have method="get". For you to access the information using $_POST you'll need to change it to method="post".
<form action="/mail.php" method="post">
<p>Name</p> <input type="text" name="name">
<p>Email</p> <input type="text" name="email">
<p>Message</p><textarea name="message" rows="6" cols="40"></textarea><br>
<input type="submit" value="Send"><input type="reset" value="Clear">
</form>
With regards to why your Bracket Live Preview is downloading the PHP file instead of running it. It sounds like your Bracket Live Preview isn't configured correctly.
You might be able to get some help from this question.
Using Brackets for PHP files

HTML PHP form post showing up blank?

I have two files, testpage.html and testpage.php in the same folder.
This is testpage.html
<!DOCTYPE HTML>
<html>
<body>
<form action="testpage.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
This is testpage.php
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?><br>
</body>
</html>
The form seems to work okay but when I hit submit nothing shows up on the next page. no matter what I enter int he form, it always reads "Welcome" "Your email address is:" with nothing entered after that like its supposed to.
Do I have something configured incorrectly? Am I using the wrong browser (firefox)?
Thanks!
"I am accessing it via file:///C:/... should I be accessing it some other way instead?"
Just as I thought.
There you go. A web browser will not parse PHP directives that way.
It must be accessed as http://localhost/yourfile.xxx
Plus, a webserver and PHP need to be installed in order to parse PHP directives.
"I don't think I have Xampp or any virtual server set up... I'll look into that, any tips on where to start?"
You need to install one. Here are a few links to get you started and depending on the platform you are using.
https://www.apachefriends.org/
http://www.wampserver.com/
https://www.mamp.info
http://www.easyphp.org/
Pick your flavour.

PHP POST not working correctly

When I submit my form, I get sent to the correct file as specified in the action attribute of my form, but the PHP in the file isn't printing the variables at all... I've combed through posts of other people having the same issue, but none of their solutions fix my problem. I've stripped my code down to a simple, textbox, button, and a php file that's supposed to print the textbox value.
If it matters, I'm running this locally in chrome, not using any servers or websites yet, I'd like to get my code working locally before I upload to my server.
HTML
<html>
<form action="Submit.php" method="post">
<input type="text" placeholder="First Name" name="firstName" id="firstName" required>
</br></br>
<input type="submit" name="submitted" value="Submit">
</form>
</html>
PHP
<html>
<body>
Name <?php echo $_POST["firstName"]; ?><br>
</body>
</html>
All I get when I click the button is a white page with "Name" printed.
Thanks!
Running scripts in response to HTTP requests is something that's done by the webserver. If you just use local files, the script will simply be loaded into the browser as a text or HTML file, it won't be executed. You can't do form processing like this.
You need to run a local server, then access the form as http://localhost/form.html

Saving Form data dynamically

I am using the following code to save the e-mails entered in the form. But on clicking on "Submit", it is giving an internal server error. Please help in rectifying it.
Code for form.php:
<html>
<body>
<form action="email_script.php" method="POST">
<p> Email Address: <input type="text" name="email" size="30"/> </p>
<input type="submit" name="submit" value="Submit"/>
</body>
</html>
Code for email_script.php:
<? php
$email = $_POST('email');
$file = "file.html"
file_put_contents($file, $email . PHP_EQL, FILE_APPEND);
print("...");
?>
You have a gap between <? and php. This should be <?php.
Also, $email = $_POST('email'); should be $email = $_POST['email']; (with square brackets)
Also, as pointed out below, the second line needs a semi colon at the end.
Also, I have never heard of PHP_EQL. Perhaps you mean PHP_EOL?
If you are working in a local environment, consider putting error_reporting(-1); at the top of your php file (after the <?php). This will give more detailed errors than just 'Internal Server Error'. BUT REMEMBER to remove the line before putting the script in a live environment.
ALSO - be aware that just adding data from a form into a file on the server, without validating or cleaning, is very very dangerous. I won't go into more detail as that's not what your question asked, but maybe look into that a little as well.

PHP Script won't load

I'm new to web development and have been wrestling with this problem for several hours now, so I've decided to turn to your wisdom. I'm trying to design a little webpage with a database for my wife to store her recipes in, but I'm having trouble getting form submission to work. Here is the code for the webpage where I take the form information in:
<html><body>
Enter the information below to add a new ingredient for use in your recipes.
<form action="add_to_database.php" method="post">
Name: <input name="name" type="text" />
Serving: <input type="text" name="serving" />
Calories: <input type="text" name="calories" />
<input type="submit" />
</form>
</body></html>
And here is some silly code I've been trying to display on the page to see if I can even get the form submission to work:
<html><body>
<?php
$name = $_POST['name'];
echo $name."<br />";
?>
</body></html>
Unfortunately, the page comes back as completely back (after I hit the submit button). What's absolutely baffling to me is that I've copied and pasted the examples from this page into some files and everything seems to work fine. So it seems as though apache and php are working correctly, but I'm messing up somewhere along the way. My apologies in advance if this seems like a stupid question but for the life of me I can't figure it out.
Do you name the other file as add_to_database.php where the form is submitted. Instead you can test on teh same page by removing the add_to_database.php from the form action.
form action="" method="post">
Name: <input name="name" type="text" />
Serving: <input type="text" name="serving" />
Calories: <input type="text" name="calories" />
<input type="submit" />
</form>
and write the php code on the same page as
$name = $_POST['name'];
echo $name;
If this is not working for you. Create a php file named test.php and type phpinfo(); there.
Verify that your page is indeed being processed by PHP - obvious question, but does your PHP file have a .php extension? Do other things like phpinfo() or echo "Test"; work?
Check the error log in /var/log/apache2/error.log or similar (if on Linux, dunno where that'd be on Windows).
Try turning display_errors on in the PHP configuration (this is a good idea only for a development install, not a production server).
a bit of a longshot, but if you can verify that php is pro essing the page. Clean up your html, you are missing the dtd, head, and encoding. . hard to say how a browser is going to interpret a form (missing a name attribute) without these.

Categories