0

ドロップメニューオプションを選択してデータベースの値を選択した値に変更すると、次のエラーが発生します。

SyntaxError: missing ) after argument list changeGroup("email@email.com")

基本的に私がやろうとしているのは、連絡先を保存するアプリがあり、グループ化されていない連絡先セクションにドロップメニューがあり、そこから値を選択すると、値が送信され、その値を使用してデータベースが更新されます。私は使用します:

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);

?>
4

1 に答える 1

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 に置き換えることをお勧めします。PDO はインジェクション攻撃を防ぎ、近い将来には削除されません。詳細については、こちらをご覧ください

于 2013-03-10T00:08:18.017 に答える