通过php在与Ajax相同的页面中查询结果 [英] Post result from a query via php in same page with Ajax

查看:166
本文介绍了通过php在与Ajax相同的页面中查询结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的网站上有3个下拉框。用户从每个选项中选择一个选项并点击提交后,数据将发布到一个外部php文件,该文件向MySQL发出查询,然后重新加载页面并发布结果。我想使这更加花哨 - 与Ajax无重新加载页面。问题是我完全是nube。我搜索interned并尝试了几个例子,但没有结果。以下是代码:

HTML FORM:

 < form name =showpriceid =showpricemethod =postaction =<?php echo $ _SERVER ['PHP_SELF'];?>> 
< select name =countryid =country>
< option value =>选择国家< / option>
< / select>
< option value =selected =selected>选择国家和行业第一。< / option>
< / select>
< input value =Submittype =submitname =submitid =submit>
< / form>

< script type =text / javascript>
var frmvalidator = new Validator(showprice);
frmvalidator.addValidation(国家,请求,请选择国家);
frmvalidator.addValidation(industry,req,请选择行业);
frmvalidator.addValidation(质量,请求,请选择质量);
< / script>

注意:我已经移除了节省空间的选项。



外部view.prices.php:



它位于另一个文件夹中,现在我用 p>

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

现在的代码是:

  if(isset($ _ POST ['submit'])){
include('config.php');
$ con1 = mysql_connect($ server,$ username,$ password);
if(!$ con1)
{
die(< b>无法连接:< / b>。mysql_error());
}
echo'< br />< br />< table id =myTableclass =tablesorteralign =center>
< thead>
< tr>
**部分表头(8列)**

< / thead>
< tbody>';

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

$ sql =SELECT * FROM $ ind WHERE quality = $ qua AND desig = $ cou ORDER BY id ASCor die('< b> Data Insert Error:< / b>' 。mysql_error());
$ b $ echo(< tr>
**某些表结果包含从MySQL数据库取得的8个变量**
< / tr>);

if(!mysql_query($ sql,$ con1))
{
die('Error:'。mysql_error());
}
}
echo'< / tbody>
< / table>';
mysql_close($ con1);
}}
else {
echo'< div class =grid_9>
< p>< b>提示:< / b>从上面的下拉菜单中选择国家,行业和质量,然后点击提交按钮查看结果。< / p>
< / div>';
}

任何帮助都非常感谢。

解决方案

我一直在使用jQuery。

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

})


I have a form on my website with 3 drop-down boxes. After user select an option from each one and hit submit the data is posted to an external php file, that makes an query to MySQL and then the page is reloaded and result posted. I'd like to make this more fancy - with ajax without reloading the page. the problem is I'm completely nube. I search interned and tried a couple of examples but no result. Here is the code:

HTML FORM:

<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>

NOTE: I have removed the options to save space.

The external view.prices.php:

It is in another folder and now I am calling the result with

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

Present code is:

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>';
}

Any help highly appreciated.

解决方案

I use jQuery for this all the time.

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

})

这篇关于通过php在与Ajax相同的页面中查询结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆