从通过PHP与阿贾克斯同一个页面的查询结果后(Post result from a query vi

2019-10-16 16:11发布

我有我的网站上有3下拉框的形式。 用户选择后,从每一个选项,并点击提交的数据发布到外部php文件,使得一个查询到MySQL,然后重新加载页面和结果发布。 我想使这更看中的 - 使用Ajax无需刷新页面。 问题是我完全NUBE。 我寻找实习和尝试了几个例子,但没有结果。 下面是代码:

HTML表格:

<form name="showprice" id="showprice" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="country" id="country">
<option value="">Select Country</option>
</select>
<select name="industry" id="industry" onchange="setOptions(document.showprice.industry.options[document.showprice.industry.selectedIndex].value);">
<option value="">Select Industry</option>
</select>
<select name="quality" id="quality">
<option value=" " selected="selected">Select country and industry first.</option>
</select>
<input value="Submit" type="submit" name="submit" id="submit">
</form>

<script  type="text/javascript">
var frmvalidator = new Validator("showprice");
frmvalidator.addValidation("country","req","Please select country");
frmvalidator.addValidation("industry","req","Please select industry");
frmvalidator.addValidation("quality","req","Please select quality");
</script>

注:我已删除的选项,以节省空间。

外部view.prices.php:

它是在另一个文件夹,现在我打电话,结果

<?php include('includes/view.prices.php'); ?>

本次代码是:

if(isset($_POST['submit'])) {
include ('config.php');
$con1 = mysql_connect($server, $username, $password);
if (!$con1)
{
die(<b>Could not connect: </b> . mysql_error());
}
echo'<br /><br /><table id="myTable" class="tablesorter" align="center">
<thead>
<tr>
**some table headers (8 columns)**
</tr>
</thead>
<tbody>';

$cou = $_POST['country'];
$ind = $_POST['industry']; 
$qua = $_POST['quality'];

$sql = "SELECT * FROM $ind WHERE quality=$qua AND desig=$cou ORDER BY id ASC" or  die('<b>Data Insert Error:</b> ' . mysql_error());

echo("<tr>
**Some table results with 8 variables taken from the MySQL database**
</tr>");

if (!mysql_query($sql,$con1))
{
die('Error: ' . mysql_error());
}
}
echo    '</tbody>
</table>';
mysql_close($con1);
}}
else {
echo '<div class="grid_9">
<p><b>TIP:</b> Pick country, industry and quality from the drop-down above and hit "Submit" button to view results.</p>
</div>';
}

任何帮助高度赞赏。

Answer 1:

我调查的jQuery。 您将要禁用默认处理程序:

e.preventDefault();

然后使用jQuery,你可以这样做:

   $.ajax({
        type: 'POST',
        url: '',
        data: $("#showprice").serialize(),  dataType: 'json',
        success: function(data){
            if( data['status'] == 'success' )
            {           
              // Do stuff here
            } 
        }
    });

该代码假定你要返回一个JSON编码字符串。 其中jQuery的可以处理没有任何问题。



Answer 2:

我使用jQuery这一切的时候。

$(function() {
    $('#showprice').sumbit(function() {
    $.post('includes/view.prices.php', $(this).serialize(),function(data) {  
    $('#idoftag').html(data);
    })
    });

})


Answer 3:

随着从朋友一些帮助,我已经成功地做到这一点:

1)在是的形式添加的文件的头:

<script type="text/javascript" src="path-to-jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var working = false;
$('#id-of-form').submit(function(e){
e.preventDefault();
if(working) return false;
working = true;
//$('#submit').val('Sending..');
$.post('path-to-php-file-to-be-executed',$('#id-of-form').serialize(),function(msg){
working = false;
//$('#submit').val('Submit');
$('#id-of-div-where-result-will-be-outputed').html(msg);
});
});
});
</script>

2)的形式后加入用于outputed数据在div

<div id="output_div"></div>

3)在路径到PHP换执行添加:

if(isset($_POST['id-of-form-field-1']) && isset($_POST['id-of-form-field-2']) && isset($_POST['id-of-form-field-3'])) {

// some queries here

}

就这样



Answer 4:

在您的形式,引用您的当前页面的动作值...例如,如果你的网页是的index.php。 然后使用行动=“的index.php”和方法=“POST”。 在专区内,你想要的数据显示,在编写格式正确的PHP代码,并使用if($ _ POST)附上所有这些代码{ - 你的数据库检索代码 - }?>。 这意味着,您的文章操作将调用相同的页面,这将使你的周围代码的情况是真实的,因此执行。 希望这有助于它唠叨我退缩,但我这个工作。



Answer 5:

在记事本+ +,它看起来像你的{}不匹配。 他们排队的时候我删除一个模具语句后,一个在别人之上。



文章来源: Post result from a query via php in same page with Ajax