简体   繁体   English

Ajax不会发布到PHP MYSQL

[英]Ajax won't post to PHP MYSQL

I have a page that has 2 cascading Select using Ajax. 我有一个页面,其中包含2个使用Ajax级联选择的页面。 The Ajax correctly populates the second Select, however when I go to post the data to MySQL using PHP i get an error that the field populated by AJAX is "Unidentified." Ajax正确填充了第二个Select,但是当我去使用PHP将数据发布到MySQL时,我得到一个错误,即AJAX填充的字段是“ Unidentified”。 The exact error is 确切的错误是

Notice: Undefined index: probcode in ..\\input.php on line 21 注意:未定义的索引:第21行.. \\ input.php中的probcode

It does post if the Ajax call is not made and the default select is still there. 如果未进行Ajax调用并且默认选择仍然存在,它会发布。 It only errors after AJAX has replaced the DIV. 只有在AJAX替换DIV之后才会出错。

I would like to add that it is like the from element disappears from PHP even though AJAX adds it to the form. 我想补充一点,即使AJAX将from元素添加到表单中,它也将从PHP中消失。 Is there another PHP function I should use to post AJAX data to MySQL? 我应该使用另一个PHP函数将AJAX数据发布到MySQL吗?

I do not know Jquery and am looking for a way to get this to work with just JavaScript at the moment (because of time and my lack of Jquery knowledge), however if there is an easy way to add Jquery to the existing code I would definitely be interested. 我不了解Jquery,目前正在寻找一种方法来使其仅与JavaScript一起使用(由于时间和缺乏Jquery知识),但是如果有一种简单的方法可以将Jquery添加到现有代码中,我会绝对有兴趣。 Also i know the data is not secure going into MySQL at the moment, That will be addressed immediately after this. 我也知道目前数据进入MySQL是不安全的,此后将立即解决。 I am just working on a wire-frame of a specific function at the moment. 目前,我只是在研究特定功能的线框。

First the two selects. 首先两个选择。

            <tr>
                <td>Problem Type</td>
                <td><?php
                    mysql_connect("localhost", "root", "") or die("Connection Failed");
                    mysql_select_db("test")or die("Connection Failed");
                    $query = "SELECT * FROM wfprobtype ORDER BY probtype ASC";
                    $result = mysql_query($query);
                    ?>
                    <select name="probtype" id="probtype" onchange="changeContent(this.value)">
                    <?php
                    while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
                    ?>
                    <option value="<?php echo $line['ID'];?>" > <?php echo $line['probtype'];?> </option>
                    <?php
                    } 
                    mysql_close()
                    ?>
                    </select>
                </td>
                <td>Problem: </td>
                <td>
                    <div id="val2div">
                        <select name="probcode" id="probcode">
                            <option value="default"></option>
                        </select>
                    </div>
                </td>
            </tr>

This is what the Ajax calls to replace "val2div" val2.php 这就是Ajax所谓的替换“ val2div” val2.php的内容

<?php
$val2=intval($_GET['val2']);
mysql_connect("localhost", "root", "") or die("Connection Failed");
mysql_select_db("test")or die("Connection Failed");
$query = "SELECT * FROM wfprobcode WHERE typeID='$val2' ORDER BY Probcode ASC";
$result = mysql_query($query);
?>
<select name="probcode" id="probcode"  onchange="">
<?php
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>
<option value="<?php echo $line['Probcode'];?>"> <?php echo $line['Probcode'];?> </option>
<?php
}
mysql_close()
?>

Ajax Script Dny.js Ajax脚本Dny.js

function changeContent(val)
{
if (val=="")
  // if blank, we'll set our innerHTML to be blank.
  {
  document.getElementById("val2div").innerHTML="";
  return;
  }
if (window.XMLHttpRequest) // new browser
  {  
      xmlhttp=new XMLHttpRequest(); // new browser
      alert("newb");
  }
else // Old Browser
  {  
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 

  }
  // on state change
  xmlhttp.onreadystatechange=function()
  {
  // if we get a good response from the webpage, display the output
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
  {
      document.getElementById("val2div").innerHTML=xmlhttp.responseText;
  }
  }
 // GET file. 
xmlhttp.open("GET","val2.php?val2="+val, true);
xmlhttp.send();
 alert(val)
}

post to MySQL input.PHP 发布到MySQL input.PHP

    <?php
    $con = mysql_connect("localhost","root","");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }

    mysql_select_db("test", $con);

    $sql="INSERT INTO `test`.`test` (
    `ID`,
    `Dattime`, 
    `probtype`,
    `probcode`
    )
    VALUES(
    NULL,NOW(),'$_POST[probtype]','$_POST[probcode]')";

    if (!mysql_query($sql,$con))
      {
      die('Error: ' . mysql_error());
      }

mysql_close($con); ?> 

I am hoping I am just missing simple something at the moment. 我希望我现在只是缺少一些简单的东西。 This is my first time working with Ajax. 这是我第一次与Ajax合作。 I really appreciate any and all help at the moment. 此刻,我非常感谢您的帮助。

EDIT: - One final edit before I attempt to rewrite the whole thing. 编辑:-我试图重写整个事情之前的最后一次编辑。 Is there a way that using Jquery I could fix this? 有没有一种方法可以使用Jquery修复此问题? If not does anyone have a down and dirty way to make it just full postback without the AJAX? 如果不是,没有人有一种肮脏而肮脏的方式来使其完全回发而不使用AJAX吗?

I really appreciate those that have contributed so far an anyone who has any ideas at all. 我非常感谢那些迄今为止有任何想法的人所做的贡献。

Thanks in Advance! 提前致谢!

-D -D

After reviewing the code endlessly I have found that the everything works fine when using IE with the code above! 经过无休止地检查代码后,我发现将IE与上面的代码一起使用时,一切正常! The issues are directly with Firefox interpreting the AJAX return. 问题直接与Firefox解释AJAX返回有关。

in val2.php you miss closinge element for select 在val2.php中,您错过了closeinge元素以供选择

 </select>

and I guess in input.PHP, it should better be '{$_POST['probtype']}' than '$_POST[probtype]' 而且我猜在input.PHP中,它最好是'{$_POST['probtype']}'不是'$_POST[probtype]'

$sql="INSERT INTO `test`.`test` (
`ID`,
`Dattime`, 
`probtype`,
`probcode`
)
VALUES(
NULL,NOW(),'{$_POST['probtype']}','{$_POST['probcode']}')";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM