如何从包含使用php从mysql数据库返回的JSON编码html字符串的javascript数组中的每个元素中删除引号

如何从包含使用php从mysql数据库返回的JSON编码html字符串的javascript数组中的每个元素中删除引号,php,html,json,ajax,quotes,Php,Html,Json,Ajax,Quotes,使用带有pdo的data1.php文件对mysql数据库的Ajax调用返回hmtl字符串,这些字符串被放入数组中,用json编码并发送到Ajax响应函数以在html中显示。当然,数组中的元素有标记和引号,它们很难删除。到目前为止,最简单和最接近的解决方案是用javascript.replace()替换字符串中的引号。这不适用于数组数组,除非引用单个元素。这是怎么回事?这是代码 Ajax调用: function processForm_L(){ var topic = $("#topic").va

使用带有pdo的data1.php文件对mysql数据库的Ajax调用返回hmtl字符串,这些字符串被放入数组中,用json编码并发送到Ajax响应函数以在html中显示。当然,数组中的元素有标记和引号,它们很难删除。到目前为止,最简单和最接近的解决方案是用javascript.replace()替换字符串中的引号。这不适用于数组数组,除非引用单个元素。这是怎么回事?这是代码

Ajax调用:

function processForm_L(){
var topic = $("#topic").val();
// send data
$.ajax({
  url: "../php/get_data1.php",
  data: {
  topic1:topic},
  type: 'POST',
  dataType: 'json',
  success: processResult_L
}); // end .onclick
}
响应函数:

function processResult_L(data, textStatus){
    // Required Callback Function
    if(data['status']['response'] == 1){
        //if(data[0] == 1){
        table_1 = [];
        table_2 = [];
        table_3 = [];

        table_1 = data.table['a'].replace( /"/g,"");
        table_2 = data.data.replace(/"/g,"");
        table_3 = data.table['b'].replace( /"/g,"");

        //table_1 = JSON.parse(data.table['a']);
        //table_2 = JSON.parse(data.data);
        //table_3 = JSON.parse(data.table['b']);
        //console.log(table_1);
        console.log(table_2);
        //console.log(table_3);
    }//end if response == 1

    else if(data.response == 0){
        //var response = data + textStatus;
        var table_4 = data.error;
        $('#display').html(table_4);
    }//end if response==0
}//end process results
function processResult_L(data, textStatus){
// Required Callback Function
//create dom elements for table head and body
//to check variables in dev console remove 'var'(make public)
var $table = $("<table id='display_table'>");
$table.append($('<tbody>'));
//create response array
var response = {};
response = data['status']['response'];
//create row array for building table data
var r = [];
switch(response){
case 0: /*Address case */
// build table header
$table.append($('<tr>')
    .append($('<th>').text('ID'))
    .append($('<th>').text('Organization'))
    .append($('<th>').text('Contact'))
    .append($('<th>').text('City'))
    .append($('<th>').text('Street'))
    .append($('<th>').text('Unit'))
    .append($('<th>').text('Post Code'))
    .append($('<th>').text('Region'))
    .append($('<th>').text('Country'))
    .append($('<th>').text('Date')));
// build table content
for(var i = 0; i < data.table_rows.length; i++) {
r[i] = data.table_rows[i];
$table.append($('<tr>')
    .append($('<td>').text(r[i].Id))
    .append($('<td>').text(r[i].Organization))
    .append($('<td>').text(r[i].Contact_Names))
    .append($('<td>').text(r[i].City))
    .append($('<td>').text(r[i].Street))
    .append($('<td>').text(r[i].Unit))
    .append($('<td>').text(r[i].Postal_Code))
    .append($('<td>').text(r[i].Region))
    .append($('<td>').text(r[i].Country))
    .append($('<td>').text(r[i].Date))
); // end append table content
}  // end build table content
    break;
get_data1.php的查询部分

<?php
$ret_s = Array();//return status
$ret_t = Array();//return table
$ret_d = Array();//return data
$result = Array();
$row_1 = 1;

if(!empty($_POST)){

    try{

        if(!empty($_POST)){
            //connect to database and insert data
            //      include "db_connect_df.php";

            // select everything from the raw_data database
            $sql = "SELECT * FROM `raw_data`";

        // prepare the sql statement
        $stmt_s =  $db->prepare($sql);
        $stmt_s->execute();
        $result = $stmt_s->fetchAll(PDO::FETCH_ASSOC);

        //set response variable
        $ret_s['response'] = 1;

        //table header
        $ret_t['a'] ="<table    id='display_table'><tr><th>ID</th><th>Organization</th><th>Contact Names</th><th>Telephone</th><th>Email</th><th>Website</th><th>Country</th><th>Region</th><th>City</th><th>Street</th><th>Unit</th><th>Postal Code</th><th>Topic</th><th>Message</th><th>Date</th></tr>";

        //fetch each row from database as html
        foreach($result as $row){
            $ret_d[$row_1] = "<tr>" ."<td>" . $row['ID'] . "</td>" ."<td>" .
            $row['Organization'] . "</td>" ."<td>" . $row['Telephone'] . "</td>" . "<td>" . $row['Email'] . "</td>" ."<td>" . $row['Website'] . "</td>" ."<td>" . $row['Country'] . "</td>" ."<td>" . $row['Region'] . "</td>" ."<td>" . $row['City'] . "</td>" ."<td>" . $row['Street'] . "</td>" ."<td>" . $row['Unit'] . "</td>" ."<td>" . $row['Postal_Code'] . "</td>" ."<td>" . $row['Topic'] . "</td>" ."<td>" . $row['Message'] . "</td>" ."<td>" . $row['ts'] . "</td>" ."</tr>";
            $row_1++;
        }
        // end table tag
        $ret_t['b'] = "</table>";

        //echo and encode array
        echo json_encode(array('status' => $ret_s, 'table' => $ret_t,'data'=> $ret_d),JSON_HEX_QUOT|JSON_HEX_TAG);


        // echo json_encode(stripslashes_nested(array('status' => $ret_s, 'table' => $ret_t,'data'=> $ret_d)),JSON_HEX_QUOT|JSON_HEX_TAG);
        }//end connect
    }//end try 

    catch (PDOException $e) {
        $error16 = '<span class="error_s">Try again another time.</span>';
        $error17 = '<span class="error_s">ERROR:' . $e->getMessage();
        $ret_s['response'] = 0;
        $ret_s['error'] = $error16 . $error17;
        echo json_encode($ret_s);    
    } //end failure
}//end if is !empty($_POST)
?>
在响应回调函数(so x=data.data)创建的数据对象中的数据数组上使用该函数

结果未定义,并且不起作用


JSON.parse(表1)只是遇到了一个
不要在任何地方替换任何东西。构建HTML字符串时,只需添加
htmlspecialchars()

<?php
// ...
$rows = [];
foreach ($result as $r) {
    $rows[] = '<tr><td>'.htmlspecialchars($r['id'])
        .'</td><td>'.htmlspecialchars($r['phone_number'])
        .'</td></tr>';
}
header('Content-Type: application/json; encoding=utf-8');
echo json_encode(array('table_rows' => join("\n", $rows)));
但是有更好的方法来实现这一点通过JSON仅发送不带标记的数据,并在JS中生成标记:

<?php
// ...
$rows = [];
foreach ($result as $r) {
    $rows[] = [
        'id' => $r['id'],
        'phone_number' => $r['phone_number']
    ];
}
header('Content-Type: application/json; encoding=utf-8');
echo json_encode(array('table_rows' => $rows));

不要在任何地方替换任何东西。构建HTML字符串时,只需添加
htmlspecialchars()

<?php
// ...
$rows = [];
foreach ($result as $r) {
    $rows[] = '<tr><td>'.htmlspecialchars($r['id'])
        .'</td><td>'.htmlspecialchars($r['phone_number'])
        .'</td></tr>';
}
header('Content-Type: application/json; encoding=utf-8');
echo json_encode(array('table_rows' => join("\n", $rows)));
但是有更好的方法来实现这一点通过JSON仅发送不带标记的数据,并在JS中生成标记:

<?php
// ...
$rows = [];
foreach ($result as $r) {
    $rows[] = [
        'id' => $r['id'],
        'phone_number' => $r['phone_number']
    ];
}
header('Content-Type: application/json; encoding=utf-8');
echo json_encode(array('table_rows' => $rows));

PHP案例0,从ajax下拉选择器表单提交调用的文件:

if(!empty($_POST)){
//connect to database and insert data
//      include "db_connect_df.php";
include "db_connect_df_test.php";
//switch statement for mysql queries based on dropdown selected
switch($topic_2){
case "Address":
//set response variable
$ret_s['response'] = 0;
// select organization address
$sql = "SELECT IFNULL(ID, '--') AS ID,
          IFNULL(Organization, '--') AS Organization,
          IFNULL(Contact_Names, '--') AS Contact_Names,
          IFNULL(City, '--') City,
          IFNULL(Street, '--') AS Street,
          IFNULL(Unit, '--') AS Unit,
          IFNULL(Postal_Code, '--') AS Postal_Code,
          IFNULL(Region, '--') AS Region,
          IFNULL(Country, '--') AS Country,
          IFNULL(ts, '--') AS ts
          FROM `raw_data`";
 // prepare the sql statement
 $stmt_s =  $db->prepare($sql);
 $stmt_s->execute();
 $result = $stmt_s->fetchAll(PDO::FETCH_ASSOC);
 // extract table rows
 foreach($result as $row){
 $ret_d[$row_1] = [
         'Id'=> $row['ID'],
         'Organization' => $row['Organization'],
         'Contact_Names' => $row['Contact_Names'],
         'City'=> $row['City'],
         'Street'=> $row['Street'],
         'Unit' => $row['Unit'],
         'Postal_Code' => $row['Postal_Code'],
         'Region' => $row['Region'],
         'Country'=> $row['Country'],
         'Date'=> $row['ts']
];//end load data from data base
$row_1++;
}//end table data as rows
    break;
Javascript初始化文件,ajax响应函数的案例0部分:

function processResult_L(data, textStatus){
    // Required Callback Function
    if(data['status']['response'] == 1){
        //if(data[0] == 1){
        table_1 = [];
        table_2 = [];
        table_3 = [];

        table_1 = data.table['a'].replace( /"/g,"");
        table_2 = data.data.replace(/"/g,"");
        table_3 = data.table['b'].replace( /"/g,"");

        //table_1 = JSON.parse(data.table['a']);
        //table_2 = JSON.parse(data.data);
        //table_3 = JSON.parse(data.table['b']);
        //console.log(table_1);
        console.log(table_2);
        //console.log(table_3);
    }//end if response == 1

    else if(data.response == 0){
        //var response = data + textStatus;
        var table_4 = data.error;
        $('#display').html(table_4);
    }//end if response==0
}//end process results
function processResult_L(data, textStatus){
// Required Callback Function
//create dom elements for table head and body
//to check variables in dev console remove 'var'(make public)
var $table = $("<table id='display_table'>");
$table.append($('<tbody>'));
//create response array
var response = {};
response = data['status']['response'];
//create row array for building table data
var r = [];
switch(response){
case 0: /*Address case */
// build table header
$table.append($('<tr>')
    .append($('<th>').text('ID'))
    .append($('<th>').text('Organization'))
    .append($('<th>').text('Contact'))
    .append($('<th>').text('City'))
    .append($('<th>').text('Street'))
    .append($('<th>').text('Unit'))
    .append($('<th>').text('Post Code'))
    .append($('<th>').text('Region'))
    .append($('<th>').text('Country'))
    .append($('<th>').text('Date')));
// build table content
for(var i = 0; i < data.table_rows.length; i++) {
r[i] = data.table_rows[i];
$table.append($('<tr>')
    .append($('<td>').text(r[i].Id))
    .append($('<td>').text(r[i].Organization))
    .append($('<td>').text(r[i].Contact_Names))
    .append($('<td>').text(r[i].City))
    .append($('<td>').text(r[i].Street))
    .append($('<td>').text(r[i].Unit))
    .append($('<td>').text(r[i].Postal_Code))
    .append($('<td>').text(r[i].Region))
    .append($('<td>').text(r[i].Country))
    .append($('<td>').text(r[i].Date))
); // end append table content
}  // end build table content
    break;
函数处理结果(数据、文本状态){
//必需的回调函数
//为表头和表体创建dom元素
//要在开发人员控制台中检查变量,请删除“var”(公开)
var$table=$(“”);
$table.append($('');
//创建响应数组
var响应={};
响应=数据['status']['response'];
//为生成表数据创建行数组
var r=[];
开关(响应){
案例0:/*地址案例*/
//生成表标题
$table.append($('')
.append($('').text('ID'))
.append($('').text('Organization'))
.append($('').text('Contact'))
.append($('').text('City'))
.append($('').text('Street'))
.append($('').text('Unit'))
.append($('').text('Post Code'))
.append($('').text('Region'))
.append($('').text('Country'))
.append($('').text('Date'));
//生成表内容
对于(var i=0;i
PHP案例0,从ajax下拉选择器表单提交调用的文件:

if(!empty($_POST)){
//connect to database and insert data
//      include "db_connect_df.php";
include "db_connect_df_test.php";
//switch statement for mysql queries based on dropdown selected
switch($topic_2){
case "Address":
//set response variable
$ret_s['response'] = 0;
// select organization address
$sql = "SELECT IFNULL(ID, '--') AS ID,
          IFNULL(Organization, '--') AS Organization,
          IFNULL(Contact_Names, '--') AS Contact_Names,
          IFNULL(City, '--') City,
          IFNULL(Street, '--') AS Street,
          IFNULL(Unit, '--') AS Unit,
          IFNULL(Postal_Code, '--') AS Postal_Code,
          IFNULL(Region, '--') AS Region,
          IFNULL(Country, '--') AS Country,
          IFNULL(ts, '--') AS ts
          FROM `raw_data`";
 // prepare the sql statement
 $stmt_s =  $db->prepare($sql);
 $stmt_s->execute();
 $result = $stmt_s->fetchAll(PDO::FETCH_ASSOC);
 // extract table rows
 foreach($result as $row){
 $ret_d[$row_1] = [
         'Id'=> $row['ID'],
         'Organization' => $row['Organization'],
         'Contact_Names' => $row['Contact_Names'],
         'City'=> $row['City'],
         'Street'=> $row['Street'],
         'Unit' => $row['Unit'],
         'Postal_Code' => $row['Postal_Code'],
         'Region' => $row['Region'],
         'Country'=> $row['Country'],
         'Date'=> $row['ts']
];//end load data from data base
$row_1++;
}//end table data as rows
    break;
Javascript初始化文件,ajax响应函数的案例0部分:

function processResult_L(data, textStatus){
    // Required Callback Function
    if(data['status']['response'] == 1){
        //if(data[0] == 1){
        table_1 = [];
        table_2 = [];
        table_3 = [];

        table_1 = data.table['a'].replace( /"/g,"");
        table_2 = data.data.replace(/"/g,"");
        table_3 = data.table['b'].replace( /"/g,"");

        //table_1 = JSON.parse(data.table['a']);
        //table_2 = JSON.parse(data.data);
        //table_3 = JSON.parse(data.table['b']);
        //console.log(table_1);
        console.log(table_2);
        //console.log(table_3);
    }//end if response == 1

    else if(data.response == 0){
        //var response = data + textStatus;
        var table_4 = data.error;
        $('#display').html(table_4);
    }//end if response==0
}//end process results
function processResult_L(data, textStatus){
// Required Callback Function
//create dom elements for table head and body
//to check variables in dev console remove 'var'(make public)
var $table = $("<table id='display_table'>");
$table.append($('<tbody>'));
//create response array
var response = {};
response = data['status']['response'];
//create row array for building table data
var r = [];
switch(response){
case 0: /*Address case */
// build table header
$table.append($('<tr>')
    .append($('<th>').text('ID'))
    .append($('<th>').text('Organization'))
    .append($('<th>').text('Contact'))
    .append($('<th>').text('City'))
    .append($('<th>').text('Street'))
    .append($('<th>').text('Unit'))
    .append($('<th>').text('Post Code'))
    .append($('<th>').text('Region'))
    .append($('<th>').text('Country'))
    .append($('<th>').text('Date')));
// build table content
for(var i = 0; i < data.table_rows.length; i++) {
r[i] = data.table_rows[i];
$table.append($('<tr>')
    .append($('<td>').text(r[i].Id))
    .append($('<td>').text(r[i].Organization))
    .append($('<td>').text(r[i].Contact_Names))
    .append($('<td>').text(r[i].City))
    .append($('<td>').text(r[i].Street))
    .append($('<td>').text(r[i].Unit))
    .append($('<td>').text(r[i].Postal_Code))
    .append($('<td>').text(r[i].Region))
    .append($('<td>').text(r[i].Country))
    .append($('<td>').text(r[i].Date))
); // end append table content
}  // end build table content
    break;
函数处理结果(数据、文本状态){
//必需的回调函数
//为表头和表体创建dom元素
//要在开发人员控制台中检查变量,请删除“var”(公开)
var$table=$(“”);
$table.append($('');
//创建响应数组
var响应={};
响应=数据['status']['response'];
//为生成表数据创建行数组
var r=[];
开关(响应){
案例0:/*地址案例*/
//生成表标题
$table.append($('')
.append($('').text('ID'))
.append($('').text('Organization'))
.append($('').text('Contact'))
.append($('').text('City'))
.append($('').text('Street'))
.append($('').text('Unit'))
.append($('').text('Post Code'))
.append($('').text('Region'))
.append($('').text('Country'))
.append($('').text('Date'));
//生成表内容
对于(var i=0;i
这很有效。谢谢。而不是测试。htmlspecialchars()在最初的php上,将表单构造移到客户端并构建jquery dom元素。在php和javascript init中添加了switch语句,以从表中选择不同的查询。在mysql查询中用字符串替换空值是毫无意义的,在php行构建函数中可能会更有效n、 但它是有效的。下面是switch语句的案例0和供其他人查看的工作代码。从技术上讲,这个问题没有得到回答,但提供了一个更好的方法。大概,要从通过ajax调用返回的json数据中删除双引号,可以使用上面提供的for循环,并使用.re循环通过第二维度数组place()。这很有效。谢谢。而不是测试。htmlspecialchars()在最初的php上,将表单构造移到客户端并构建jquery dom元素。在php和javascript init中添加了switch语句,以从表中选择不同的查询。在mysql查询中用字符串替换空值是毫无意义的,在php行构建函数中可能会更有效n、 但它是有效的。下面是switch语句的案例0和供其他人查看的工作代码。从技术上讲,这个问题没有得到回答,但提供了一个更好的方法。大概,要从通过ajax调用返回的json数据中删除双引号,可以使用