php
  • javascript
  • sql
  • ajax
  • menu
  • 2013-03-10 69 views 0 likes 
    0

    當我選擇一個下拉菜單選項,在我的數據庫更改值在選擇的值時,它提供此錯誤:錯誤使用下拉菜單來更改數據庫值

    SyntaxError: missing) after argument list changeGroup("[email protected]") 
    

    基本上什麼即時試圖做的是我有一個存儲聯繫人的應用程序,在未分組的聯繫人部分,它有下拉菜單,當你從中選擇一個值時,它提交值並使用該值來更新數據庫。我使用:

    action='javascript:changeGroup(".$contactDetails.") 
    

    告訴更新聲明哪個聯繫人更新。

    我的代碼:

    <!--Include Database connections info--> 
    <?php include('config.php'); ?> 
    
    <!--Links to CSS file for formatting--> 
    <link href="Contacts.css" rel="stylesheet" type="text/css"/> 
    
    <!--Links to Javascript file for the for action to change the group of a contact--> 
    <script src="ajax.js" language="javascript"></script> 
    
    <?php 
    
    $contactDetails = $_GET['contactDetails']; 
    
        $cdquery="SELECT * FROM `contacts` WHERE `newEmail` = '$contactDetails'"; 
        $cdresult=mysql_query($cdquery) or die ("Query to get data from first table failed: ".mysql_error()); 
    
        while ($row = mysql_fetch_assoc($cdresult)) 
        { 
    
        echo "" . $row['newFname'] . " " . $row['newLname'] . "'s " . "Details:"; 
        echo "<table>"; 
        echo "<tr>"; 
        echo "<th>Name:</th>"; 
        echo "<th>Email Address:</th>"; 
        echo "<th>Phone:</th>"; 
        echo "<th>Postal Address:</th>"; 
        echo "<th>Group:</th>"; 
        echo "</tr>"; 
    
         echo "<tr>"; 
         echo "<td>" . $row['newFname'] . " " . $row['newLname'] . "</td>"; 
         echo "<td>" . $row['newEmail'] . "</td>"; 
         echo "<td>" . $row['newPhone'] . "</td>"; 
         echo "<td>" . $row['newAddress'] . "</td>"; 
         echo "<td>" . $row['group'] . "</td>"; 
         echo "</tr>"; 
        } 
        echo "</table>"; 
    
        echo "<form action='javascript:changeGroup(".$contactDetails.")' method='get'> Add contact to 
        <select id='group' name='group' onchange='this.form.submit(value=this.options[this.selectedIndex].value)'> 
        <option>Select a group...</option> 
        <option value='Family'>Family</option> 
        <option value='Friends'>Friends</option> 
        <option value='Colleagues'>Colleagues</option></select> 
        group.</form>"; 
    
    mysql_close($link); 
    
    ?> 
    

    AJAX功能:

    function changeGroup(str) 
        { 
        document.getElementById("content02").innerHTML=""; 
        if (str=="") 
        { 
        document.getElementById("content02").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("content02").innerHTML=xmlhttp.responseText; 
        document.getElementById("content02").innerHTML = ""; 
        } 
        } 
        xmlhttp.open("GET",'getChangeGroup.php?contactChange='+contactChange+'&group='+group,true); 
        xmlhttp.send(); 
        xmlhttp.onreadystatechange = changeReload; 
        xmlhttp.send(null); 
        } 
    

    PHP:

    <!--Include Database connections info--> 
    <?php include('config.php'); ?> 
    
    <!--Links to CSS file for formatting--> 
    <link href="Contacts.css" rel="stylesheet" type="text/css"/> 
    
    <?php 
    
    $contactChange = $_GET['contactChange']; 
    $group = $_GET['group']; 
    
    $cdquery="UPDATE `contacts` SET `group` = '$group' WHERE `newEmail` = '$contactChange'"; 
    $cdresult=mysql_query($cdquery) or die ("Query to get data from first table failed: ".mysql_error()); 
    
    mysql_close($link); 
    
    ?> 
    
    +0

    您的代碼易受注入攻擊。 – Daedalus 2013-03-10 00:05:57

    回答

    1

    我真的不知道你的代碼做什麼,我也不要有耐心以完全複製它在我的一端,但就目前而言,你是在這一行傳遞一個變量到你的函數,而不是一個字符串:

    echo "<form action='javascript:changeGroup(".$contactDetails.")' method='get'> Add contact to 
    

    爲了解決這個問題,你需要引用您的字符串作爲實際字符串:

    echo "<form action='javascript:changeGroup(\'".$contactDetails."\')' method='get'> Add contact to 
    

    我會以爲這是你的主要問題。如果沒有這些引號,javascript會將您的回顯變量視爲js變量,而不是字符串。

    在問候你的數據庫交互...

    此外,您使用的是過時..很快就被刪除數據庫交互的API。我建議使用PDO來替換它,它可以防止注入攻擊,並且不會在近期內被移除,learn more about it here

    +0

    @Corey如果此答案解決了您的問題,請通過單擊答案旁邊的複選標記來接受此問題。 – Daedalus 2013-03-10 05:37:11

    相關問題