0

所以我的问题是我在函数(数据)部分出现错误,我不知道为什么 - 在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 创建一个 autofil 表单继承我的代码的其余部分

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>

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

4

1 回答 1

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]), 方括号中的内容是可选的。

于 2012-08-07T02:38:41.120 回答