嗨,我有一个 php 表单和一个函数,一旦表单是 sumbit,我想通过 jquery 调用它。这是我的尝试,但它似乎不起作用!url 必须是我的页面的完整 url 还是我的 php 脚本名称?我还需要将该函数附加到该 url 的末尾,以便 jquery 调用并发送该函数从表单提交的值吗?
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready( function() {
$("#search_box").submit(function() {
dataform = $("#search").val();
$.ajax({
type: "POST",
url: "tree2.php",
data: dataform,
success: function() {
alert('success!');
$('#search_box').fadeOut(100).hide();
}
})
});
});
</script>
</head>
<?php
form();
if($_POST['submit'])
process();
function form(){
echo"<div class='searchbox'>";
echo "<form method='post' id='search_box' action='$_SERVER[PHP_SELF]'>";
echo "<input type='text' id='search' name='species'/>";
echo "<input type='submit' name='submit' />";
echo "</form>";
echo "</div>";
}
function process(){
$searchvar = htmlentities($_POST['species']);
echo $searchvar;
}