Я создал html-страницу с 4 входами: имя, адрес электронной почты, пол, комментарий и одну кнопку отправки и библиотеку jquery. Я связал эту страницу со страницей CSS (style.css), одним файлом javascript (script.js), одной страницей php (validation.php). Я прикрепил свой код. Однако, когда я нажимаю кнопку отправки, я не получаю никакого ответа. Может ли кто-нибудь помочь, что не так в моем коде.
$(document).ready(function(){
$("#mybutton").click(function(){
// validation based on Regular expression (10 chars max, alpha and numeric allowed)
// Name can't be blank
$('#name2').on('input', function() {
var input=$(this);
var is_name=input.val();
if (is_name){
input.removeClass("invalid").addClass("valid");
}
else{
input.removeClass("valid").addClass("invalid");
}
});
// Email must be an email
$('#email2').on('input', function() {
var input=$(this);
var re = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/;
var is_email=re.test(input.val());
if (is_email){
input.removeClass("invalid").addClass("valid");
}
else{
input.removeClass("valid").addClass("invalid");
}
});
// Message can't be blank
$('#comm').keyup(function(event) {
var input=$(this);
var message=$(this).val();
console.info(message);
if (message){
input.removeClass("invalid").addClass("valid");
}
else{
input.removeClass("valid").addClass("invalid");
}
});
//Check if gender is empty
$('#gn').on('input', function() {
var input=$(this);
var is_gender=input.val();
if (is_gender){
input.removeClass("invalid").addClass("valid");
}
else{
input.removeClass("valid").addClass("invalid");
}
});
// JQUERY MODAL WINDOW
$.post( "validation.php", {
name: $("#name2").val(),
email: $("#email2").val(),
gender: $("#gn").val(),
comment: $("#comm").val(),
formHasbeenPosted: $("#yes").val()
})
.done(function( data ) {
$("#result").html(data);
});
return false;
});
}).maindiv{
margin:0 auto;
width:980px;
height:500px;
background:#fff;
padding-top:20px;
font-size:14px
}
.form_div{
width:70%;
float:left
}
form{
width:440px;
border:1px dashed #ccc;
padding:10px 30px 40px;
background-color:#f0f8ff;
font-family:'Droid Serif',serif
}
img
{
width:100px;
height:100px;
float: right;"
}
textarea{
width:250px;
height:60px;
border-radius:1px;
box-shadow:0 0 1px 2px #123456;
margin-top:10px;
padding:5px;
border:none
}
.input{
width:250px;
height:15px;
border-radius:1px;
box-shadow:0 0 1px 2px #123456;
margin-top:10px;
padding:5px;
border:none;
margin-bottom:20px
}
.button{
color:#fff;
border-radius:3px;
background:#1F8DD6;
padding:5px;
margin-top:40px;
border:none;
width:100px;
height:30px;
box-shadow:0 0 1px 2px #123456;
font-size:16px;
position:center
}
.error{
color:red
}
.error_show{
color: red;
margin-left: 10px;
}
input.invalid, textarea.invalid{
border: 2px solid red;
}
input.valid, textarea.valid{
border: 2px solid green;
}
.radio{
width:15px;
height:15px;
border-radius:1px;
margin-top:10px;
padding:5px;
border:none;
margin-bottom:20px
}
.formget{
float:right;
margin-top:85px
}<!DOCTYPE html>
<html>
<head>
<link href = "style.css" rel = "stylesheet">
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src = "script.js"></script>
</head>
<body>
<div class = "maindiv">
<div class = "form_div">
</div>
<form id = "myForm">
<h2>Form</h2>
<img src = "C:\wamp64\www\FormValidation\onelife.png" alt = "Logo"><br>
<span class = "error">* required field.</span><br>
Name:
<input class = "input" id = "name2" name = "name" type = "text" value = "">
<span class = "error">*</span> </br>
E-mail:
<input class = "input" id = "email2" name = "email" type = "text" value = "">
<span class = "error">*valid email address is required </span></br>
Gender:
<input class = "radio" id = "gn" name = "gender" type = "radio" value = "female">Female
<input class = "radio" id = "gn" name = "gender" type = "radio" value = "male">Male
<span class = "error">*Please select a gender</span></br>
Comment:
<textarea cols = "40" id = "comm" name = "comment" rows = "5">
</textarea><br/>
<input class = "submit" id = "mybutton" type = "button" value = "Submit"><br/>
<div id = "result"></div>
</form>
</div>
</body>
</html>Validation.php
<!DOCTYPE HTML>
<html>
<body>
<?php
$nameError = "";
$emailError = "";
$genderError = "";
if (isset($_POST['formHasbeenPosted'])){
if (empty($_POST["name2"])) {
$nameError = "Name is required";
} else {
$name = test_input($_POST["name2"]);
// check name for alphanumeric values
if (!preg_match("/^[a-zA-Z0-9]*$/",$name)) {
$nameError = "Only alphanumeric values allowed";
}
}
if (empty($_POST["email2"])) {
$emailError = "Email is required";
} else {
$email = test_input($_POST["email2"]);
// check if e-mail address syntax is valid or not
if (!preg_match("/([w-]+@[w-]+.[w-]+)/",$email)) {
$emailError = "Invalid email format";
}
}
//Check if comment is empty
if (empty($_POST["comm"])) {
$comment = "";
} else {
$comment = test_input($_POST["comm"]);
}
//Check if gender is empty
if (empty($_POST["gn"])) {
$genderError = "Gender is required";
} else {
$gender = test_input($_POST["gn"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
</body>
</html>





попробуйте повторить что-то на validation.php, потому что, насколько я вижу, там ничего не печатается. попробуйте этот код
if (isset($_POST['formHasbeenPosted'])){
if (empty($_POST["name2"])) {
$nameError = "Name is required";
echo $nameError;
} else {
$name = test_input($_POST["name2"]);
// check name for alphanumeric values
if (!preg_match("/^[a-zA-Z0-9]*$/",$name)) {
$nameError = "Only alphanumeric values allowed";
echo $nameError ;
}
}
if (empty($_POST["email2"])) {
$emailError = "Email is required";
echo $emailError ;
} else {
$email = test_input($_POST["email2"]);
// check if e-mail address syntax is valid or not
if (!preg_match("/([w-]+@[w-]+.[w-]+)/",$email)) {
$emailError = "Invalid email format";
echo $emailError ;
}
}
//Check if comment is empty
if (empty($_POST["comm"])) {
$comment = "";
} else {
$comment = test_input($_POST["comm"]);
}
//Check if gender is empty
if (empty($_POST["gn"])) {
$genderError = "Gender is required";
echo $genderError ;
} else {
$gender = test_input($_POST["gn"]);
}
if ($nameError == "" && $emailError == "" && $genderError == ""){
echo "Data is valid, you can process";
}
}