将PHP中的数组转换为javascript函数时遇到问题,该函数被注入回网页


trouble getting an array in PHP into a javascript function that is injected back to the web page

在JQuery对话框中,我向用户显示他们可以访问的数据库中的帖子列表。这来自第一个PHP脚本。用户选择一个帖子,然后由第二个PHP脚本在数据库中执行操作。这是部分工作,但我无法将要处理的数组POSTed发送到第二个PHP脚本,该脚本使用数组数据执行查询。

该序列从jQuery-ajaxPOST开始。Firebug控制台在POST中向我显示正确的数据,并将它们列为数组元素,例如field[]abc、field[]xyz等。第一个PHP editPost.PHP接收一个字符串,该字符串有效,第二个数组为

$propertyFields = array();
$propertyFields = $_POST[ 'propertyFields' ];

在第一个脚本的查询使用字符串(而不是数组)完成后,while循环会构建选择项的列表,并填充一个javascript函数,该函数在某种程度上起作用。它提供了列表描述,但不包括propertyFields数组——应该是第二个PHP脚本。

while($row = mysql_fetch_array($fetch)) {
    $activeCount = mysql_real_escape_string($row["activeCount"]);
    $error_NumberOfActives = "<ul>Welcome back.
    <br>Click the 'Edit this one' button that you want to edit or change.
    <script type='text/javascript'>var properTFields=[]; //added array declaration,but no fix
    function retrievePost( idx, propertyID, properTFields ) {
//retrieve database content for clicked post and changes text message inside the button 
    alert( properTFields + '|in editPost' ); //test - get - function Array() {[native code]}|in editPost
    jQuery.ajax({
        url: '../phpFunctions/editPostUpdate.php',  
        data:{  properTID: propertyID, //POSTs OK
                propertyFields: properTFields //propertyFields is empty in POST to editPostUpdate.php                          },                                   
        type:'POST',                                       
        success: function (data) {
            if (data ) {
            //button reads 'can now be changed' from editPostUpdate.php with forced success from editPostUpdate.php.
                document.getElementById(idx).innerHTML = propertyID + ' can now be changed';
            //alert(data); //get forced success message from editPostUpdate.php
            } else {
            document.getElementById(idx).innerHTML = propertyID + ' has errors,check,try again';
            }
        }
    });
    }</script></ul>";
//some messages are constructed in PHP objects - nothing to do with this problem
    //make HTML list items of active postings for 3Step countries
    $idMaker = "active" . $rowCount;
    //this syntax is exceptionally picky. It took advice from gurus and hours of trials to get it. It works with the retrievePost function above.
    //test - $propertyFields was added as a function variable. It doesn't work. 
    $error_ListActives = "<li> $propertyID, $storedStreetAddress, $storedCity3
    <button type='button' id='$idMaker' onclick='retrievePost( id, $propertyID, $propertyFields )'>Edit this one</button>
    </li>"; 

请注意,在列表项中,propertyID、storedStreetAddress和storedCity3的变量都在上面的retrievePost函数注入的HTML中工作和显示。只是$propertyFields数组不起作用。在POST数据中,Firebug控制台显示properPID的字符串和propertyFields数组的空字段。

当用户通过按钮进行选择时,我希望将propertyFields数组POST到第二个PHP脚本editPostUpdate.PHP中,如图所示,其中的数据数组将在查询中使用。此POST发生,但它是一个空数组。我尝试了很多东西,包括原始的javascript数组作为连接到数据的元素:propertyFields:No success。该怎么办?

问题根源的线索是"填充一个有效的javascript函数。它提供了列表描述,但不能包含propertyFields数组"。那么数组出了什么问题??这花了我很长时间,但我发现在这种情况下Javascript无法理解字符串数组。我误以为字符串会更简单。当我用JSON_encode()命令bang将properFields数组更改为JSON时,如上所述,代码第一次尝试就成功了。故事的寓意:将JSON与Javascript结合使用。