我遇到的问题是ajax只是将变量附加到当前URL的末尾。我需要它指向ProHost.php。
我正在使用此处的教程tutorial
我使用它的方式是来自表单的onSubmit="return validateInput();
验证我的输入,然后调用function ajaxFunction()
将数据发送到我的php文件ProHost.php
但是现在ajax就是这样做的
NewHost.php?FName=d&LName=d&email=samesc%40yahoo.com&BirthDate=2014-06-8&Pass=s&PassRe=s
什么时候看起来像这样
ProHost.php?FName=d&LName=d&email=samesc%40yahoo.com&BirthDate=2014-06-18&Pass=s&PassRe=s
如果我自己编辑URL(ProHost.php?FName=d&LName=...
),PHP文件就会在空白页面上以纯文本形式报告。
以下是HTML表单:
<form action="" method ="get" onSubmit="return validateInput(); " >
<p id="demo" style="text-align: center">…</p>
First Name: <input Id="FName" name="FName" type="text" required placeholder="john" size="35" value="d" >
Last Name: <input Id="LName" name="LName" type="text" required placeholder="Smith" size="35" value="d">
Email: <input Id="email" name="email" type="email" required placeholder="John@Smith.com" size="35" >
Birth Date: <input Id="BirthDate" name="BirthDate" type="date" required min="1900-01-01" >
Password: <<input Id="Pass" name="Pass" type="password" required size="35" placeholder="Secret" >
Retype Password: <input Id="PassRe" name="PassRe" type="password" required size="35" placeholder="Secret" >
<input type="reset" class="Button" value="Clear" name="ResetBtn">
<input type="submit" class="Button" value="Submit">
<input id="clickMe" type="button" value="clickme" onclick="doFunction();" />
</form>
这是脚本:
<script language="javascript" type="text/javascript">
function ajaxFunction(){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
}catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
}catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
}catch (e){
// Something went wrong
alert("Your browser broke! Please use a current web browser");
return false;
}
}
}
// Create a function that will receive data
// sent from the server and will update
// div section in the same page.
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('demo');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
// Now get the value from user and pass it to
// server script.
//Mine
var Fname = document.getElementById("FName").value;
var Lname = document.getElementById("LName").value;
var BirthDate = document.getElementById("BirthDate").value;
var email = document.getElementById("email").value;
var Pass = document.getElementById("Pass").value;
var queryString = "?Fname=" + Fname + "&Lanme=" + LName +
"&BirthDate=" + BirthDate + "&EMail=" + email + "&Pass=" + Pass;
ajaxRequest.open("GET", "ProHost.php" + queryString, true);
ajaxRequest.send(null);
return false
}
答案 0 :(得分:3)
尝试更改此行:
ajaxRequest.open("GET", "ProHost.php" + queryString, true);
对此:
ajaxRequest.open("GET", "/ProHost.php" + queryString, true);
使用/