Searched the forum. Sending html form data to php file

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
beads
New php-forum User
New php-forum User
Posts: 2
Joined: Fri Apr 19, 2019 10:16 am

Fri Apr 19, 2019 10:34 am

I have a static website with two functioning pages. The second page is a contact form. I have tried to code it so that the POST method will return the info to the user from process-form.php (my filename). When I submit the form, I get a 404 Not Found Error. Being a newbie, I'm not quite sure where my problem is. I don't know if it is a problem with my php, which I admittedly don't understand well, or if it is something concerning my server, nginx. All files are in the nginx root directory. Code for both files follows:

surveyForm.html

Code: Select all

<!DOCTYPE html, css, php>

<style>
  
  h1 {
    background-color: #000000       ;
    color: #FFFFFF                   ;
    text-align: center;
  }
  p {
    color:                    ;
  }
  body {
    background-color:         ;
  }
  form {
    background-color:         ;
    color:                    ;
  }
</style>

<html>
  
  <h1 id="title">Contact Details</h1><br>
  
  <div id="main">
    
    <div id="description">
      <p id="description">Please fill in all information below and you will recive an email with all requested information</p>
    </div>
    <br>
    
    <div id="body">
      
      <form action="process-form.php" name="survey-form" method="post">
        <label for "name">Name <input type="text" id="name-label" placeholder="Name"></label><br><br>
        <label for "email">Email <input type="email" id="email-label" placeholder="email"></label><br><br>
        <label for="number"> How many times have you visited a secular/freethought website in the last month? (1 to 10) <input type="number" id="number-label" value="0" min="0" max="10"></label><br><br>
        <label for "radio">What information can we provide? (Select all that apply)</label><br><br>
          <label for "Atheism"><input type="radio" id="Atheism"> Atheism</label><br>
      <label for "Humanism"><input type="radio" id="Humanism"> Humanism</label><br>
        <label for "Skepticism"><input type="radio" id="Skepticism"> Skepticism</label><br>        
        <label for "List"><input type="radio" id="List"> US and UK Atheist/Humanist organizations</label><br>
        <label for "skepticList"><input type="radio" id="skepticList"> US and UK Skeptic organizations</label><br><br>
        <button type="submit">Submit</button>        
        
and process-form.php:

Code: Select all

 <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Contact Form</title>
</head>
<body>
    <h1>Thank You</h1>
    <p>Here is the information you have submitted:</p>
    <ol>
        <li><em>Name:</em> <?php echo $_POST["name-label"]?></li>
        <li><em>Email:</em> <?php echo $_POST["email-label"]?></li>
        <li><em>number:</em> <?php echo $_POST["number-label"]?></li>
        <li><em>Atheism:</em> <?php echo $_POST["Atheism"]?></li>
        <li><em>Humanism:</em> <?php echo $_POST["Humanism"]?></li>
        <li><em>Skepticism:</em> <?php echo $_POST["Skepticism"]?></li>
        <li><em>List</em> <?php echo $_POST["List"]?></li>
        <li><em>skepticList</em> <?php echo $_POST["skepticList"]?></li>
    </ol>
</body>
</html>

Please, rather than just give me the code, if your answer could include a little bit of why?

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 839
Joined: Mon Feb 22, 2016 5:52 pm

Fri Apr 19, 2019 6:09 pm

Ok, first things first:

if the file cannot be found, then it can't be in the same directory, I've just copy pasted them into a trial directory and it can find the file.

And now the rest:

A basic html file should use the following-

Code: Select all

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Contact Form</title>
</head>
<body>

<!-- code goes here -->

</body>
</html>
Which is what you have in process-form.php and NOT in your surveyForm.html

Also in process-form.php,you have assumed that the user will always supply you with some input, in the case of radio inputs, you will only get an input if the button is selected.

Never trust user input. What you need to do is check for input and then make sure it is what you expect:

Code: Select all

<?php
if (isset($_POST["Atheism"]){
  // It is set, so do your coding here
} else {
  // Do whatever here for no input
}
Additional reading on validation

beads
New php-forum User
New php-forum User
Posts: 2
Joined: Fri Apr 19, 2019 10:16 am

Sat Apr 20, 2019 6:21 am

Thank you very much. I'll give that a try. Thank you for the link, I've bookmarked that site for further study and reference. As a newbie, I really appreciate it when people answer my question with pointers to how I can learn the concept, not just copy and paste someone else's solution. Thanks,

beads

Post Reply