我想将一个输入类型元素发送到没有表单提交的PHP页面,只能使用javascript。我尝试使用 $。ajax ,但没办法。我不知道它是语法还是逻辑错误。有人告诉我要做一个PHP页面而不是两个。
HTML页面(但是.php)
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="../../js/jquery-3.2.0.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style>
#prova{
padding-right: 1%;
padding-top: 1%;
}
</style>
<script>
function ajax_post(apri){
var stile = "top=10, left=10, width=650, height=600, status=no, menubar=no, toolbar=no scrollbars=no";
// Create our XMLHttpRequest object
var hr = new XMLHttpRequest();
// Create some variables we need to send to our PHP file
var url = "sperem.php";
var fn = document.getElementById("id_buyers").value;
alert(fn);
var vars = "postid="+fn;
alert(vars);
hr.open("POST", url, true);
// Set content type header information for sending url encoded variables in the request
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// Access the onreadystatechange event for the XMLHttpRequest object
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
var return_data = hr.responseText;
document.getElementById("status").innerHTML = return_data;
}
}
// Send the data to PHP now... and wait for response to update the status div
hr.send(vars); // Actually execute the request
/*window.open(apri, "", stile);*/
document.getElementById("status").innerHTML = "processing...";
}
</script>
</head>
<body>
<div class="container">
<center><h1>Add Orders:</h1></center>
<form class="form-inline" name="ordine">
<div class="form-group" id="prova">
<label for="Buyers">ID_Buyers:</label>
<input type="text" class="form-control" placeholder="Enter IDBUYERS" id="id_buyers">
</div>
<div class="form-group" id="prova">
<input type="submit" id="bottone" value="show" onClick="ajax_post('sperem.php')"/>
</div>
</form>
<br><div id="status"></div>
</div>
</body>
</html>
这是 Php页面:
<?php
$dato = $_POST['postid'];
echo($dato);
?>
答案 0 :(得分:1)
从
更改此内容input type="submit"
到
input type="button"
然后你可以获得你在文本框中给出的数据。
这里的问题是表单是在浏览器上运行时提交的,就像你想要运行ajax一样,你需要运行页面而不需要提交/重新加载。
希望它对你有所帮助。