如何在没有ajax,json的情况下完成javascript验证时运行php文件

Currently for school I have to make a contact form with Javascript validation that will check whether name, email etc is filled in. If so, it has to run a php code to send the email ofcourse.

Right now I finished all the javascript validation, but I'm a bit stuck regarding on how to activate php code to send the email after the javascript validation is finished..

I'm not allowed to use anything else like AJAX, Jquery.. I've seen many posts about this, but they were all about these languages..

I wasn't planning about redirecting and using a landing page after submitting the form.

For this one, you need to reach back in history a bit and use techniques that predate AJAX. Basically, old-school forms from the 90's.

  1. In your HTML, make sure your form has id and name attributes. The typical convention is to give them the same value and make them unique on the page
  2. Make sure all your form fields are inside the form element in your HTML
  3. Set the form's action attribute to the address of your PHP page. The address will vary depending on how your application is set up
  4. Set the form's method to POST. In your PHP page, you should use the standard mechanism to read POST data from the form
  5. Lastly, you need something in your javascript code to cause the form to activate. A typical low-tech way of doing this is to use getElementByID() to get a reference to your form in the DOM, and then call submit() on it. This technique does not require any javascript libraries like jquery. Of course, this should be done after you run your validation code.

If you need help on how to do the steps, just do a bit more googling and you'll find plenty of details.

The AJAX techniques that libraries like JQuery use are also doing something similar to a form POST, however it's smarter in that you don't need to leave the page you're on. This was a new feature that browsers started to adopt over 10 years ago. My guess is your professor wants to teach you the fundamentals first before diving into the sophisticated stuff. Good luck!

Yes Its Possible You Can Just Use Some Css /Java Script Code And All Done

You Can Make 2 input type Of button And Set onlick and Call The JavaScript junction And Hide Button Using Css Display None Property And Click Button Using JavaScript When Validation Is Finished Using If else Condition

Like This

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<style>
input{display:none;}
</style>
<body>
<input type="submit" value="" id="test" onclick="name()">
<input type="submit" value="" id="test2" onclick="name2()">

<script>

if (new Date().getHours() > 18) {
    document.getElementById("test").click();
}
else
{
 document.getElementById("test2").click();
}
function name(){
alert('My Name Is Imtiyaz GO www.trickwordpress.in');
} 
function name2(){

alert('for More Help Go "www.trckwordpress.in"');
}

</script>


</body>
</html>

</div>