我可以在一个脚本中使用多个功能吗? [英] Can I use more than one function in one script?

查看:85
本文介绍了我可以在一个脚本中使用多个功能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图一起使用Ajax,PHP和MySQL来构成表单元素,下拉样式选择输入能够选择一个选项,并使ajax通过脚本将数据发送到数据库并返回给页面到一个单独的div.我已经弄清楚了大部分代码.我现在的问题是这个.

I'm trying to use Ajax, PHP, and MySQL together to make a form element, drop-down style select input be able to select an option and have the ajax sent data through the script to the database and back to the page to a separate div. I have most of the code for this figured out. My issue now is this.

我有3个下拉菜单,其中1个菜单要转到该脚本.然后将根据从哪个菜单中选择三个不同的$ GET脚本.

I have three drop-down menus that 1 would like to have go to this script. It would then go to three different $GET scripts depending on which menu it was selected from.

<script type="text/javascript">
function showUser(str)
{ 
if (str=="")
  { document.getElementById("txtHint").innerHTML="";
  return; }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest(); }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
  xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText; }
  }
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send(); }
</script>

我拥有的三个选择元素都带有onchange函数.

The three select elements I have are accompanied with onchange functions.

<select name="characters" onchange="showCharacter(this.value)">
<select name="towers" onchange="showTowers(this.value)">
<select name="enemies" onchange="showEnemies(this.value)">

此脚本的PHP代码如下:

The PHP code I have for this script is this:

<?php

include("dbinfo.inc.php");

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
mysql_select_db("db1801445-main", $con);
$sql="SELECT * FROM user WHERE id = '".$q."'";
$result = mysql_query($sql);
$q=$_GET["q"];

echo "<table border='1'>
    <tr>
    <th>Name</th>
    <th>Description</th>
    </tr>";
while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['Name'] . "</td>";
  echo "<td>" . $row['Description'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?> 

Idk还能怎么做,以便你们能帮助我吗?

Idk how else to do this so can you guys help me?

推荐答案

我同意使用jQuery将使您的生活更轻松,但是要解决您的问题:

I agree that using jQuery would make your life easier, but to address your question:

您可以使该JS代码更通用,从而更易于重用:

You could make that JS code more general so it would be easier to reuse:

<script type="text/javascript">
function doAjax(ajaxFunction, str)
{ 
if (str=="")
  { document.getElementById("txtHint").innerHTML="";
  return; }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest(); }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
  xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText; }
  }
xmlhttp.open("GET","getuser.php?q="+str+"func="+ajaxFunction,true);
xmlhttp.send(); }
</script>

然后更改您的PHP即可:

And then change you PHP do this:

<?php
$sFunction  = $_GET["func"];
$sQ =$_GET["q"];

switch($sFunction){

    case 'showUser':
       //code here;
    break;
    case 'showTowers':
       //code here;
    break;
   case 'showEnemies':
       //code here;
    break;
    case 'showCharacters':
       //code here;
    break;
}

?>

然后回到前端:

<select name="characters" onchange="doAjax('showCharacter',this.value)">
<select name="towers" onchange="doAjax('showTowers',this.value)">
<select name="enemies" onchange="doAjax('showEnemies',this.value)">

这篇关于我可以在一个脚本中使用多个功能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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