gpt4 book ai didi

javascript - 即使我在提交按钮上使用 jQuery,页面也会重新加载

转载 作者:行者123 更新时间:2023-11-27 23:44:26 25 4
gpt4 key购买 nike

这是索引页:

<?php
// Initialize variables to null.
$nameError ="";
$emailError ="";
$genderError ="";
$websiteError ="";
// On submitting form below function will execute.


if(isset($_POST['submit'])){
if (empty($_POST["name"])) {
$nameError = "Name is required";
} else {
$name = test_input($_POST["name"]);
// check name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameError = "Only letters and white space allowed";
}
}

if (empty($_POST["lastname"])) {
$nameError = "Name is required";
} else {
$name = test_input($_POST["lastname"]);
// check name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameError = "Only letters and white space allowed";
}
}



if (empty($_POST["email"])) {
$emailError = "Email is required";
} else {
$email = test_input($_POST["email"]);
// check if e-mail address syntax is valid or not
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) {
$emailError = "Invalid email format";
}
}


if (empty($_POST["phonenumber"])) {
$nameError = "phone number is required";
} else {
$name1 = test_input($_POST["phonenumber"]);
// check name only contains letters and whitespace
if (!preg_match("/^[0-9 ]*$/",$name1)) {
$phoneerror = "Only numbers and white space allowed";
}
}

if (empty($_POST["section"])) {
$selectError = "Choose the required field";
} else {
$select = test_input($_POST["section"]);
}


if (empty($_POST["section1"])) {
$selectError = "Choose the required field";
} else {
$select = test_input($_POST["section1"]);
}
}

function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>

<!DOCTYPE html>
<html>
<head>
<title>Job Test</title>
<link href="style.css" rel="stylesheet">
<script src = "http://code.jquery.com/jquery-1.11.1.min.js"> </script>
<script src = "script1.js"></script>

</head>
<body>
<div class="maindiv">
<div class="form_div">
<div class="title">
<h2>Find Out More About Computing</h2>
</div>

<form method="post" action="index.php">
Name: <br />
<input class="input" name="name" type="text" value="" required placeholder="Enter name">
<span class="error">* <?php echo $nameError;?></span>
<br />
Last Name: <br />
<input class="input" name="lastname" type="text" value="" required placeholder="Enter last name">
<span class="error">* <?php echo $nameError;?></span>
<br />
E-mail: <br />
<input class="input" name="email" type="text" value="" required placeholder="Enter Email">
<br />
<span class="error">* <?php echo $emailError;?></span>
<br />
Mobile Phone Number: <br />
<input class="input" name="phonenumber" type="text" value="" required placeholder="Enter Phone Number">
<span class="error">* <?php echo @$phoneerror;?></span>
<br />


Citizenship: <br />
<select class="select" name="section" required >
<option value="None" >None</option>
<option value="Austrilian" >AUSTRALIA</option>
<option value="india" >INDIA</option>
<option value="others" >OTHERS</option>
</select>
<span class="error">*<?php echo @$selectError;?></span>

<br />

Are You a Permanent Resident of Australia
<br />
<select class="select" name="section1" required >
<option value="None" >None</option>
<option value="YES" >YES</option>
<option value="NO" >NO</option>

</select>
<span class="error">*<?php echo @$selectError;?></span>

<br />


<button class="submit" id="submit" name="submit" type="button" value="submit">Submit</button>
</form>
</div>
</body>
</html>

这是表单 index.php 的 html 页面

这是提交按钮上的 jQuery 函数。不要重新加载页面并定向到 submit.php

      $(document).ready(function() {


$('#submit').click(function(e){
// e.preventDefault();
// var page = $(this).attr('id');
// $('#content').location.reload('content/submit.php');
// alert('ok');
});


//function submit_form()
//{
// var page = $(this).attr('id');
window.location.reload('content/submit.php');
//}

});

此功能未运行。当我按下提交按钮时,它会再次重新加载页面index.php

提交.php

   <div id="content">
<h2 style="color:#FF0000;"> Thanks For the Submission </h2>
</div>

请帮助我..我被困在这里

最佳答案

正如您所说,这是一个工作测试,我不会为您编码,但会告诉您哪里出了问题。首先,您的索引文件和验证文件需要是两个单独的文件。原因是我们将对 validate.php 文件进行 AJAX 调用,并且您希望它返回一个值而不返回索引页面的其余部分。

$('#submit').click(validateData());

function httpGet(theUrl){
//FETCH Data From Server
xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET", theUrl , false );
xmlhttp.send();
return xmlhttp.responseText;
}


function validateData(){
serverResponse="validate.php?"+httpGet$("#myForm").serialize();
if(serverResonse=="Input Valid"){
$("#responseDiv").html("HTML FOR SUCESSFULL VALIDATION");
$("#formDiv").css("display","none");

}else{
$("#responseDiv").html("VALIDATION ERRORS");

}

$("#responseDiv").css("display","inline");


}
 <!DOCTYPE html>
<html>
<head>
<title>Job Test</title>
<link href="style.css" rel="stylesheet">
<script src = "http://code.jquery.com/jquery-1.11.1.min.js"> </script>
<script src = "script1.js"></script>

</head>
<body>

<div class="maindiv">
<div id="responseDiv"></div>
<div id="formDiv" class="form_div">
<div class="title">
<h2>Find Out More About Computing</h2>
</div>

<form id='myForm'>
Name: <br />
<input class="input" name="name" type="text" value="" required placeholder="Enter name">
<span class="error">* <?php echo $nameError;?></span>
<br />
Last Name: <br />
<input class="input" name="lastname" type="text" value="" required placeholder="Enter last name">
<span class="error">* <?php echo $nameError;?></span>
<br />
E-mail: <br />
<input class="input" name="email" type="text" value="" required placeholder="Enter Email">
<br />
<span class="error">* <?php echo $emailError;?></span>
<br />
Mobile Phone Number: <br />
<input class="input" name="phonenumber" type="text" value="" required placeholder="Enter Phone Number">
<span class="error">* <?php echo @$phoneerror;?></span>
<br />


Citizenship: <br />
<select class="select" name="section" required >
<option value="None" >None</option>
<option value="Austrilian" >AUSTRALIA</option>
<option value="india" >INDIA</option>
<option value="others" >OTHERS</option>
</select>
<span class="error">*<?php echo @$selectError;?></span>

<br />

Are You a Permanent Resident of Australia
<br />
<select class="select" name="section1" required >
<option value="None" >None</option>
<option value="YES" >YES</option>
<option value="NO" >NO</option>

</select>
<span class="error">*<?php echo @$selectError;?></span>

<br />


< button class="submit" id="submit" name="submit" type="button" value="submit">Submit</button>
</form>
</div>
</body>

现在,在您的 validate.php 文件中,如果它返回“Input Valid”,那么您的 jquery 将显示responseDiv 并将其innerHTML 设置为您设置为成功验证html 的内容。

关于javascript - 即使我在提交按钮上使用 jQuery,页面也会重新加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33358812/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com