javascript代码错误 - 函数(数据)错误,我不知道为什么

时间:2012-08-07 02:32:51

标签: php javascript json

所以我的问题是我在函数(数据)部分出现错误,我不知道为什么 - 在Dreamweaver中我收到错误它只是显示红色标记说错误代码

<script language="javascript" type="text/javascript">
    $("#dropdown").on('change', function() {//when you select something from the dropdown function run and will switch the data
        $.post("backgroundScript.php", {
                uid: $(this).val()
            } function(data) { // right here is the error im getting
                 $("#first").val(data.first);
               $("#last").val(data.last);
               // etc.;
            }, 'json'
        );
    });
</script>

我想要做的是从db继承我的代码的其余部分创建一个自动文件

try {  
  # MySQL with PDO_MYSQL  
  $DBH = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);  
  $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

  //$DBH->prepare('SELECT first FROM contacts');
}  
catch(PDOException $e) { 
    echo "I'm sorry, I'm afraid I can't do that.";  
    file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);   
}  
//get query
$FNresult=$DBH->query('SELECT first FROM contacts'); 
//set fetch mode
$FNresult->setFetchMode(PDO::FETCH_ASSOC);

$dropdown = "<select name='contacts' id='contacts' >";

while($row =$FNresult->fetch()) {

  $dropdown .= "\r\n<option value='{$row['first']}'>{$row['first']}</option>";
 // echo getLN();

}

$dropdown .= "\r\n</select>";

echo $dropdown;

//}
/*
//                  Get last name

function getLN(){
    $query = "SELECT last FROM contacts";
    $LNresult=mysql_query($query);

    $last;
    while($row = mysql_fetch_assoc($LNresult)) {

        $last = "{$row['last']}";

    }
    echo $last;
}//end getLN
*/

$DBH = null; 
?>
<!-- javascript on client-side -->
<script language="javascript" type="text/javascript">
    $("#dropdown").on('change', function() {//when you select something from the dropdown function run and will switch the data
        $.post("backgroundScript.php", {
                uid: $(this).val()
            } function(data) {
                 $("#first").val(data.first);
               $("#last").val(data.last);
               // etc.;
            }, 'json'
        );
    });
</script>


<script language="javascript" type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js">
//$("#[id]");
</script>

<form action="insert.php" method="post">
First Name: <input type="text" id="first" name="first"><br>
Last Name: <input type="text" id="last"><br>
Phone: <input type="text" id="phone"><br>
Mobile: <input type="text" id="mobile"><br>
Fax: <input type="text" id="fax"><br>
E-mail: <input type="text" id="email"><br>
Web: <input type="text" id="web"><br>
<input type="Submit">
</form>

如果您还需要其他任何东西,请告诉我,我非常感谢,感谢一下!

1 个答案:

答案 0 :(得分:0)

您在,的“数据”部分之后错过了.post()

<script>
    $("#dropdown").on('change', function() {
        $.post(
            "backgroundScript.php", 
            {
                uid: $(this).val()
            }, // you were missing this comma
            function(data) {
                $("#first").val(data.first);
                $("#last").val(data.last);
                // etc.;
            }, 
            'json'
        );
    });
</script>

请注意,.post()的格式为:

$.post(url, [data], [callback], [callback type]),方括号中的内容是可选的。