Jump to content

PHP code will not process form submission from website


ttnaddy16

Recommended Posts

On my website I have a form that uses the post method to submit data and my PHP code will not process the form submission. I am almost positive that my form code Is all correct but the PHP code I'm very unsure about. What I would like my PHP code to do Is process the form data and then possibly export that data to a text file so I can view the text file when I want to. If I could just get the PHP code to even process the data that would be great for starters. My PHP code right now just displays errors with variables.I know how to use Xampp So that's not the problem. Any help with this would be greatly appreciated. I would really like to try to get this problem solved soon because I would like to post my website soon. I do not have much experience in PHP coding and should be considered a beginner. Included will be my website code and my PHP code. 

Website Code-

<!DOCTYPE html>
<html>
<body>
<hr>
<h2>Computer Repair Form</h2>
</div>

<div>

<div>
<form>

<form action="submiiresults.php" method="post">

<div>

<input type="text" name="firstname" placeholder="First Name" required/>

</div>

<div>

<input type="text" name="lastname" placeholder="Last Name" required/>

</div>

<div>

<input type="text" name="email" placeholder="Email" required/>

</div>

<div>

<input type="text" name="subject" placeholder="Subject" required/>

</div>

<div>
<div>

<input type="checkbox" name="harddrivecrashed" value="Hard Drive Crashed">
<label>Hard Drive Crashed</label><br>

<input type="checkbox" name="hasvirus" value="Has Virus">
<label>Has Virus</label><br>

<input type="checkbox" name="needsoperatingsystem" value="Needs Operating System">
<label>Needs Operating System</label>
<br><br>
</div>

<div>
<input type="checkbox" name="needsmicrosoftoffice" value="Needs Microsoft Office">
<label>Needs Microsoft Office</label><br>

<input type="checkbox" name="interestedinbackupservices" value="Interested In Backup Services">
<label>Interested In Backup Services</label><br>

<input type="checkbox" name="wantsacustompcbuild" value="Wants A Custom PC Build">
<label>Wants A Custom PC Build</label>
<br><br>

<input type="submit" name="submit">
<input type="reset" value="Reset">
</form>
<br><br>
</div>
</div>
</form>
</div>
</body>
</html>

 

PHP Code-

<!DOCTYPE html>
<html>
<body>

<?php
 // Check if the form is submitted
if ( isset( $_POST['submit'] ) ) {

// retrieve the form data by using the element's name attributes value as key

echo '<h2>form data retrieved by using the $_REQUEST variable<h2/>'

$firstname = $_REQUEST['firstname'];
$lastname = $_REQUEST['lastname'];
$email = $_REQUEST['email'];
$subject = $_REQUEST['subject'];
$harddrivecrashed = $_REQUEST['harddrivecrashed'];
$hasvirus = $_REQUEST['hasvirus'];
$needsoperatingsystem = $_REQUEST['needsoperatingsystem'];
$needsmicrosoftoffice = $_REQUEST['needsmicrosoftoffice'];
$interestedinbackupservices = $_REQUEST['interestedinbackupservices'];
$wantsacustompcbuild = $_REQUEST['wantsacustompcbuild'];
// display the results
echo 'Form Data ' . $lastname .' ' . $firstname .' ' .$email .' ' . $subject .' ' . $harddrivecrashed .' ' . $hasvirus .' ' . $needsoperatingsystem .' ' . $needsmicrosoftoffice .' ' . $interestedinbackupservices .' ' . $wantsacustompcbuild;

// check if the post method is used to submit the form

if ( filter_has_var( INPUT_POST, 'submit' ) ) {

echo '<h2>form data retrieved by using $_POST variable<h2/>'

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$harddrivecrashed = $_POST['harddrivecrashed'];
$hasvirus = $_POST['hasvirus'];
$needsoperatingsystem = $_POST['needsoperatingsystem'];
$needsmicrosoftoffice = $_POST['needsmicrosoftoffice'];
$interestedinbackupservices = $_POST['interestedinbackupservices'];
$wantsacustompcbuild = $_POST['wantsacustompcbuild'];

// display the results
echo 'Form Data ' . $lastname .' ' . $firstname .' ' .$email .' ' . $subject .' ' . $harddrivecrashed .' ' . $hasvirus .' ' . $needsoperatingsystem .' ' . $needsmicrosoftoffice .' ' . $interestedinbackupservices .' ' . $wantsacustompcbuild;
}

// check if the get method is used to submit the form

if ( filter_has_var( INPUT_GET, 'submit' ) ) {

echo '<h2>form data retrieved by using $_GET variable<h2/>'

$firstname = $_GET['firstname'];
$lastname = $_GET['lastname'];
$email = $_GET['email'];
$subject = $_GET['subject'];
$harddrivecrashed = $_GET['harddrivecrashed'];
$hasvirus = $_GET['hasvirus'];
$needsoperatingsystem = $_GET['needsoperatingsystem'];
$needsmicrosoftoffice = $_GET['needsmicrosoftoffice'];
$interestedinbackupservices = $_GET['interestedinbackupservices'];
$wantsacustompcbuild = $_GET['wantsacustompcbuild'];
}

// display the results
echo 'Form Data ' . $lastname .' ' . $firstname .' ' .$email .' ' . $subject .' ' . $harddrivecrashed .' ' . $hasvirus .' ' . $needsoperatingsystem .' ' . $needsmicrosoftoffice .' ' . $interestedinbackupservices .' ' . $wantsacustompcbuild;
exit;
}
?>

</body>
</html>

 

Link to comment
Share on other sites

you have two nested <form> tags and the second one is ignored by the browser. since the first one doesn't contain a method='post' attribute, the data is submitted as $_GET data and will be submitted to the same URL the form is on.

remove the first <form> tag and the form will submit $_POST data to the URL that you have in the action='...' attribute.

Link to comment
Share on other sites

Ha ha ha I do see that now can't believe I missed that so I did take out that form the top one and I took out the second form at the bottom 2 so now I only 2 form tags the opening and closing form tag. But now when I submit my form it gives me this error Parse error: syntax error, unexpected '$firstname' (T_VARIABLE), expecting ',' or ';' in C:\xampp\htdocs\submitresults.php on line 13, I really have no idea what this error is. 

Link to comment
Share on other sites

a php syntax error means you basically turned in a writing assignment and it received red mark(s) on it for being written wrong.

the error means that php encountered a variable, while it was still expecting some closing punctuation. if you look at the lines of code leading up to the line where the error is being reported, you will notice that the echo '....' statement on the line above is missing the closing ;  

 

Link to comment
Share on other sites

Yup you are right there were 3 echoes in total that did not have semicolons so I: after them, I added the 3 semicolons to the 3 echo statements but now when I submit my form code Nothing processes My PHP code is just shown It does not actually run and output any echos here's my modified PHP code- 

<!DOCTYPE html>
<html>
<body>

<?php
 // Check if the form is submitted
if ( isset( $_POST['submit'] ) ) {

// retrieve the form data by using the element's name attributes value as key

echo '<h2>form data retrieved by using the $_REQUEST variable<h2/>';

$firstname = $_REQUEST['firstname'];
$lastname = $_REQUEST['lastname'];
$email = $_REQUEST['email'];
$subject = $_REQUEST['subject'];
$harddrivecrashed = $_REQUEST['harddrivecrashed'];
$hasvirus = $_REQUEST['hasvirus'];
$needsoperatingsystem = $_REQUEST['needsoperatingsystem'];
$needsmicrosoftoffice = $_REQUEST['needsmicrosoftoffice'];
$interestedinbackupservices = $_REQUEST['interestedinbackupservices'];
$wantsacustompcbuild = $_REQUEST['wantsacustompcbuild'];
// display the results
echo 'Form Data ' . $lastname .' ' . $firstname .' ' .$email .' ' . $subject .' ' . $harddrivecrashed .' ' . $hasvirus .' ' . $needsoperatingsystem .' ' . $needsmicrosoftoffice .' ' . $interestedinbackupservices .' ' . $wantsacustompcbuild;

// check if the post method is used to submit the form

if ( filter_has_var( INPUT_POST, 'submit' ) ) {

echo '<h2>form data retrieved by using $_POST variable<h2/>';

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$harddrivecrashed = $_POST['harddrivecrashed'];
$hasvirus = $_POST['hasvirus'];
$needsoperatingsystem = $_POST['needsoperatingsystem'];
$needsmicrosoftoffice = $_POST['needsmicrosoftoffice'];
$interestedinbackupservices = $_POST['interestedinbackupservices'];
$wantsacustompcbuild = $_POST['wantsacustompcbuild'];

// display the results
echo 'Form Data ' . $lastname .' ' . $firstname .' ' .$email .' ' . $subject .' ' . $harddrivecrashed .' ' . $hasvirus .' ' . $needsoperatingsystem .' ' . $needsmicrosoftoffice .' ' . $interestedinbackupservices .' ' . $wantsacustompcbuild;
}

// check if the get method is used to submit the form

if ( filter_has_var( INPUT_GET, 'submit' ) ) {

echo '<h2>form data retrieved by using $_GET variable<h2/>';

$firstname = $_GET['firstname'];
$lastname = $_GET['lastname'];
$email = $_GET['email'];
$subject = $_GET['subject'];
$harddrivecrashed = $_GET['harddrivecrashed'];
$hasvirus = $_GET['hasvirus'];
$needsoperatingsystem = $_GET['needsoperatingsystem'];
$needsmicrosoftoffice = $_GET['needsmicrosoftoffice'];
$interestedinbackupservices = $_GET['interestedinbackupservices'];
$wantsacustompcbuild = $_GET['wantsacustompcbuild'];
}

// display the results
echo 'Form Data ' . $lastname .' ' . $firstname .' ' .$email .' ' . $subject .' ' . $harddrivecrashed .' ' . $hasvirus .' ' . $needsoperatingsystem .' ' . $needsmicrosoftoffice .' ' . $interestedinbackupservices .' ' . $wantsacustompcbuild;
exit;
}
?>

</body>
</html>

 

Link to comment
Share on other sites

if you are seeing the php code in the browser, it means that the php language engine is not being invoked. given that you were previously getting php errors from this code, about the only thing that comes to mind is if you switched how you are opening the  page the form is on.

you should be using a URL like - http://localhost/your_form.php (or .htm or .html) to open the form. If you are clicking directly on the file where the form code is saved, you are actually opening the file in the browser, rather than going through the web server. if the form is being opened through the file system, when you submit it, the .php page will be opened directly in the browser through the file system too, not through the web server.

Link to comment
Share on other sites

First thing, you need to check the Request Method, not hope a button will be submitted in order for your script to work. That will completely fail in certain cases. You need to do this:

if ($_SERVER['REQUEST_METHOD'] == 'POST'){
//process form
}

Next, do not create variables for nothing. You already have the POST/GET variables, just use them.

Link to comment
Share on other sites

Yep you are right using localhost in the address bar instead of clicking the file worked ? I also got rid of their request variables because you're right I don't need them. Now if I wanted my PHP code to output this data to a text file and then keep adding to it for further form submissions how would I go about that? Basically, to this point, I've kinda learned about echos and variables for PHP coding but beyond that, I'm not really certain. The reason I would like it output to text files that I could check it every once in awhile and find out information from that file. Better yet is there a way that PHP code can send an automatic email to my email address so I can get constant updates of submissions?

Link to comment
Share on other sites

to log your own information to a file, use file_put_contents() with the FILE_APPEND flag.

Quote

send an automatic email

in programming, there is no 'automatic', the computer only does exactly what someone has written code to tell it to do.

to send an email from your .php script, you can use php's mail() function or one of the 3rd party mail classes - phpmailer or swiftmailer

Link to comment
Share on other sites

I used the PHP mail to function and I keep getting this error Warning: mail(): SMTP server response: 553 We do not relay non-local mail, sorry. in C:\xampp\htdocs\submitresults.php on line 50  I believe this has to do with the from address I'm supposed to send this email from but I don't get what email I should use 'cause I'm technically not sending it from my only email account.

<!DOCTYPE html>
<html>
<body>

<?php
 // Check if the form is submitted
if ( isset( $_POST['submit'] ) ) {

// check if the post method is used to submit the form

if ( filter_has_var( INPUT_POST, 'submit' ) ) {

echo '<h2>Form data retrieved by using $_POST variable<h2/>';

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$harddrivecrashed = $_POST['harddrivecrashed'];
$hasvirus = $_POST['hasvirus'];
$needsoperatingsystem = $_POST['needsoperatingsystem'];
$needsmicrosoftoffice = $_POST['needsmicrosoftoffice'];
$interestedinbackupservices = $_POST['interestedinbackupservices'];
$wantsacustompcbuild = $_POST['wantsacustompcbuild'];

// display the results
echo 'First Name:' . $firstname .' <br>Last Name:' . $lastname .' <br>Email Address:' .$email .' <br>Subject:' . $subject .' <br>Needs/Problems:<br>' . $harddrivecrashed .' <br>' . $hasvirus .' <br>' . $needsoperatingsystem .' <br>' . $needsmicrosoftoffice .' <br>' . $interestedinbackupservices .' <br>' . $wantsacustompcbuild;
$to = "ttnaddycomputerrepair@gmail.com";
$subject = "Tyler Naddy Computer Repair Form Submission";

$message = "
<html>
<head>
<title>Tyler Naddy Computer Repair Form Submission</title>
</head>
<body>
<?php echo 'First Name:' . $firstname .' <br>Last Name:' . $lastname .' <br>Email Address:' .$email .' <br>Subject:' . $subject .' <br>Needs/Problems:<br>' . $harddrivecrashed .' <br>' . $hasvirus .' <br>' . $needsoperatingsystem .' <br>' . $needsmicrosoftoffice .' <br>' . $interestedinbackupservices .' <br>' . $wantsacustompcbuild;
?>
</body>
</html>
";

// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

// More headers
$headers .= 'From: <webmaster@example.com>' . "\r\n";

mail($to,$subject,$message,$headers);
}
exit;
}
?>

</body>
</html>

 

Link to comment
Share on other sites

I tried phpmailer but i'm not sure where to  include the phpmailer code can't i include  it in submitresults.php that way it will include submitted from variables here's my new php code but it gives me an error now. I did include the vendor folder it references.

<!DOCTYPE html>
<html>
<body>

<?php
 // Check if the form is submitted
if ( isset( $_POST['submit'] ) ) {

// check if the post method is used to submit the form

if ( filter_has_var( INPUT_POST, 'submit' ) ) {

echo '<h2>Form data retrieved by using $_POST variable<h2/>';

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$harddrivecrashed = $_POST['harddrivecrashed'];
$hasvirus = $_POST['hasvirus'];
$needsoperatingsystem = $_POST['needsoperatingsystem'];
$needsmicrosoftoffice = $_POST['needsmicrosoftoffice'];
$interestedinbackupservices = $_POST['interestedinbackupservices'];
$wantsacustompcbuild = $_POST['wantsacustompcbuild'];

// display the results
echo 'First Name:' . $firstname .' <br>Last Name:' . $lastname .' <br>Email Address:' .$email .' <br>Subject:' . $subject .' <br>Needs/Problems:<br>' . $harddrivecrashed .' <br>' . $hasvirus .' <br>' . $needsoperatingsystem .' <br>' . $needsmicrosoftoffice .' <br>' . $interestedinbackupservices .' <br>' . $wantsacustompcbuild;

/**
 * This example shows settings to use when sending via Google's Gmail servers.
 * This uses traditional id & password authentication - look at the gmail_xoauth.phps
 * example to see how to use XOAUTH2.
 * The IMAP section shows how to save this message to the 'Sent Mail' folder using IMAP commands.
 */

//Import PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;

require './vendor/autoload.php';

//Create a new PHPMailer instance
$mail = new PHPMailer;

//Tell PHPMailer to use SMTP
$mail->isSMTP();

//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;

//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6

//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;

//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';

//Whether to use SMTP authentication
$mail->SMTPAuth = true;

//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "ttnaddycomputerrepair@gmail.com";

//Password to use for SMTP authentication
$mail->Password = "password";

//Set who the message is to be sent from
$mail->setFrom('ttnaddycomputerrepair@gmail.com', 'Tyler');

//Set an alternative reply-to address
$mail->addReplyTo('ttnaddycomputerrepair@gmail.com', 'Tyler');

//Set who the message is to be sent to
$mail->addAddress('ttnaddycomputerrepair@gmail.com', 'Tyler');

//Set the subject line
$mail->Subject = 'PHPMailer GMail SMTP test';

//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML(file_get_contents('submitresults.php'), __DIR__);

//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';

//send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
    //Section 2: IMAP
    //Uncomment these to save your message in the 'Sent Mail' folder.
    #if (save_mail($mail)) {
    #    echo "Message saved!";
    #}
}

//Section 2: IMAP
//IMAP commands requires the PHP IMAP Extension, found at: https://php.net/manual/en/imap.setup.php
//Function to call which uses the PHP imap_*() functions to save messages: https://php.net/manual/en/book.imap.php
//You can use imap_getmailboxes($imapStream, '/imap/ssl') to get a list of available folders or labels, this can
//be useful if you are trying to get this working on a non-Gmail IMAP server.
function save_mail($mail)
{
    //You can change 'Sent Mail' to any other folder or tag
    $path = "{imap.gmail.com:993/imap/ssl}[Gmail]/Sent Mail";

    //Tell your server to open an IMAP connection using the same username and password as you used for SMTP
    $imapStream = imap_open($path, $mail->Username, $mail->Password);

    $result = imap_append($imapStream, $path, $mail->getSentMIMEMessage());
    imap_close($imapStream);

    return $result;
exit;
}
?>

</body>
</html>

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.