传递Java脚本unicode数组

时间:2018-08-09 01:53:52

标签: php

我将unicode值存储在java脚本数组中,但是当我将其传递给ci控制器时,它没有以正确的语言显示。 如何使用表单发布将javascript unicode数组传递给php? 我的代码是:-

var myTableArray = [];
$("table#search_result_table tr").each(function() {
var arrayOfThisRow = [];
var tableData = $(this).find('td');
if (tableData.length > 0) {
    tableData.each(function() { arrayOfThisRow.push($(this).text()); });
    myTableArray.push(arrayOfThisRow);
}
});
var myJSON = JSON.stringify(myTableArray);
$.post("<?php echo base_url("Purchase/addnew"); ?>",{data:     
myJSON},$("#purform").serialize(),function(data)

1 个答案:

答案 0 :(得分:0)

Santosh,要通过AJAX和JSON发布Unicode数组,您需要3个文件,即Javascript文件,html文件和php文件。下面是样本代码,

JS文件

// make the AJAX request
// #dataform : it is a html data form id
var dataString = $('#dataform').serialize();	

$.ajax({
	type: "POST",
	url: 'php_file.php',
	data: dataString,
	dataType: 'json',
	success: function (data) {
	    if (data.success == 0) {
		var errors = '';
		if (data.err_msg != '')
		    alert('Error');
	    }
	    else if (data.success == 1) {
		alert('Success');
	    }
	},
	error: function (x,e) {
		alert('Error: '+x.status+','+x.responseText);
	}
});

HTML文件

<form id="dataform" name="dataform" method="post" action="" role="form">
	<input type="text" name="field1" id="field1" />
	<input type="text" name="field2" id="field2" />
	<input type="text" name="field3" id="field3" />
	<input type="text" name="field4" id="field4" />
	<button type="button" name="submit" id="submit" onclick="return false;">Submit</button>
</form>

PHP文件

$field1=$_REQUEST["field1"];
$field2=$_REQUEST["field2"];
$field3=$_REQUEST["field3"];
$field4=$_REQUEST["field4"];

//Your Validation Logic 
$return_array = validate($field1);
if($return_array['success'] == '1') {
    //Your SQL Query //
}

function validate($field1)
{
    $return_array = array();
    $return_array['success'] = '1';
    $return_array['err_msg'] = '';

    //Validate Field Logic
    if($field1=='')
    {
        $return_array['success'] = '0';
        $return_array['err_msg'] = 'Field1 is required!';
    }

    return $return_array;
}

header('Content-type: text/json');
echo json_encode($return_array);
die();