PHP Form Help

Hello everyone. I was hoping some of your smarter then me people can help me with a project. I am learning HTML and other stuff with it such as Java, scrips, PHP etc. I am attempting to get this form to work for a website I am working on. however I am kind of stuck on how I can get the form to send. I will do my best to post the HTML of the form, and the code from the file dependencies. Bare with me and thanks for taking the time to teach me how to do this for next time. Essentially, i think I need to write a PHP file? However suggestions are welcome.

Here is my code from the actual HTML page. This is just the part of the pages code however I think its the most important as its my contact form it self.

                    <div class="form-group">
                        <input name="user_name" type="text" class="form-control" placeholder="Your Name">
                    </div>
                    <div class="form-group">
                        <input name="user_email" type="text" class="form-control" placeholder="Email Address">
                    </div>
                    <div class="form-group">
                        <input name="user_subject" type="text" class="form-control" placeholder="Subject">
                    </div>
                </div>
            </div>
            <div class="col-md-6 col-sm-12">
                <div class="block">
                    <div class="form-group-2">
                        <textarea name="user_message" class="form-control" rows="3" placeholder="Your Message"></textarea>
                    </div>
                        <button class="btn btn-default" type="submit">Send Message</button>
                </div>
            </div>
            <div class="error" id="error">Sorry, we seemed to of caused a wipe. Please try again or email us directly.</div>
            <div class="success" id="success">Message Sent!</div>
        </form>
    </div>

Furthermore, here is the code from the dependency file I have named script.js here is the code I have so far that ties to the form on my contact.html page from above.

$(’#contact-form’).validate({
rules: {
user_name: {
required: true,
minlength: 4
},
user_email: {
required: true,
email:true
},
user_subject: {
required: false,
},
user_message: {
required: true,
},
},
messages: {
user_name: {
required: “Come on, you have a name don’t you?”,
minlength: “Your name must consist of at least 2 characters”
},
user_email: {
required: “Please insert your email address”,
},
user_message: {
required: “Hmmm, your not following mechanics very well. what seems to be going on?”,
minlength: “Your name must consist of at least 2 characters”
},

 },
 submitHandler: function(form) {
  $(form).ajaxSubmit({
     type:"POST",
     data: $(form).serialize(),
     url:"sendmail.php",
     success: function() {
        $('#contact-form #success').fadeIn();
     },
     error: function() {

        $('#contact-form #error').fadeIn();
     }
  });

}});

Now I was following a tutorial and wrote this best of my abilities and understanding. I think the issue is with the sendmail.php url. It seems to be calling a PHP file maybe? However I dont have this file. Or perhaps this was a placeholder for the tutorial? I think that’s the issue but have no idea how to write a PHP file for the script file to call to. Any suggestions? Again sorry for my poor code and describing this but any help would be awesome. Trying to learn.

I think the website somehow messed up the code. I attached screen shots as well below.


What happens when you click on submit? have you included jQuery? if nothing happens, can you check the console in the browser? it should throw an error there since you are using jQuery.

I would suggest starting with a plain php form to start with and understand how it works before trying to do this with JavaScript.
Below is a very basic PHP form with the same fields as yours that posts to itself. I’m not on my development machine at the moment so I haven’t tested it.

<form id="contact-form" method="post" onsubmit="<?php echo $_SERVER['PHP_SELF'] ?>">
  <div class="form-group">
    <input name="user_name" type="text" class="form-control" placeholder="Your Name" />
  </div>
  <div class="form-group">
    <input name="user_email" type="text" class="form-control" placeholder="Email Address" />
  </div>
  <div class="form-group">
    <input name="user_subject" type="text" class="form-control" placeholder="Subject" />
  </div>
  <div class="form-actions">
    <button type="submit">Post this form</button>
  </div>
</form>
<?php if (isset($_POST['user_name'])): ?>
  <div class="form-field-results">
      <div class="result">
        <?php
          // Note that I am assuming these are being passed back here for this example.
          // You should do validation for each of them similar to the if statement .
        ?>
        Your Name: <?php echo $_POST['user_name'] ?> <br /><br />
        Email Address: <?php echo $_POST['user_email'] ?> <br /><br />
        Subject: <?php echo $_POST['user_subject'] ?> <br /><br />
      </div>
  </div>
<?php else: ?>
  No post values have been passed to this form.
<?php endif; ?>

use a codeblock like this:

```PHP

```

```JavaScript

```

```HTML

```

Yes and no.
You need two parts for it:

  1. An action= and method= parameter on the <form> element you have surrounding your form (assuming you have one already, if not it’s time you get one). Details on MDN.

  2. The actual PHP script that handles the input from your form. How you use the variables depends on the method= parameter specified in 1. If you’re using get you need to use the $_GET Super-Global Array in PHP, if you’re using post (recommended in most cases) you’d need to use the $_POST Super-Global Array in PHP. The array keys will correspond to your input’s name= attribute.

Now, as for your JavaScript.
Having client-side validation is all nice and dandy, but what prevents someone from using curl (or any other method of sending arbitrary data really) to your site and circumventing client-side checking altogether?
The answer is basically nothing. Always validate inputs on the server side. Client-side validation is nice to give users a hint for wrong data or formats entered into the form, but should never be your final checks for the input, especially if it goes to any kind of database. The same is true for the auto validation attributes that HTML gives you. They do the same thing that the JavaScripts do in a native way, but don’t solve the problem either.

  1. HTML form
<form method="POST" action="processor.php">
<input name="age" value="4"/>
<button>submit</button>
</form>
  1. PHP procesor.php
/// ini vars
$age = 0;

if (isset($_POST["age"]){
     $age = $_POST["age"];
}

if ($age >0){
   echo("you are over the age of zero! you are " . $age); 
} else {
 echo("You should use this script correctly age = 0");
}

POST, GET, INSERT, DELETE +
are HTTP METHODS There are sent as headers to every page… every web page browsing is done via GET… but when you press submit it can be different things… eg POST, INSERT, DELETE etc… JavaScript can also affect this.

NOTE: All code is typed from the top of my head an not into a code editor so I’ve not validated it for typos :stuck_out_tongue:

Thanks everyone I will give this a shot.