decoding with jquery .serialize()
I all. I posted yesterday a question regarding dates not sent with jquery-ajax and found a solution with the .serialize() function.
Unfortunately the serialize() function 开发者_运维问答decodes the date inputs and the result I get is this:
arrival= 13%2F04%2F2011 &departure= 28%2F04%2F2011 &ap_ID=2
I know how use decodeURIComponent() for single fields post like this:
var arrivalVal = $('#arrival').val();
arrivalVal = decodeURIComponent(arrival);
but how do I use it for the long string made by the .serialize() function?
Sorry if this seems a pretty obvious question but can't get my way around it.
try with that
var editserialize = $('form#edit').serialize();
editserialize = decodeURIComponent(editserialize.replace(/%2F/g, " "))
...
For php you can do urldecode
function http://php.net/manual/en/function.urldecode.php
function decodeSerialize($QUERY_STRING, $valueSpecChars = false)
{
$array = array();
$a = explode('&',$QUERY_STRING);
$i = 0;
while ($i < count($a)) {
$b = explode('=', $a[$i]);
$ar = preg_replace('/\[/', '][', htmlspecialchars(urldecode($b[0])), 1);
$ar = str_replace(array('[',']'), array('["','"]'), $ar);
$val = urldecode($b[1]);
if($valueSpecChars) $val = htmlspecialchars($val);
else $val = str_replace('"', '\"', $val);
eval('$array["'.$ar.'="'.$val.'";');
$i++;
}
return $array;
}
this function returns an array exactly as $_POST.
Try This:
<?php
/*
* As a Class File your-class-file.php
* Decode jQuery Serialize Post String For PHP
*/
class PHP_Serialize {
function Decode_Serialize($QUERY_STRING) {
/*
* Decode form.serelize() jQuery Post String
* Return like $_POST['Form_Input_Name or ID']
*/
$a = explode('&', $QUERY_STRING);
$i = 0;
$store = array();
while ($i < count($a)) {
$b = explode('=', $a[$i]);
$array_name = htmlspecialchars(urldecode($b[0]));
$array_value = htmlspecialchars(urldecode($b[1]));
$store[$array_name] = $array_value;
$i++;
}
/*
* Convert Array as an Object
* return(object)$store;
* Use ........Object->Form_Input_Name or ID
* or
* Use as An Array .........$var["Form_Input_Name or ID"]
*/
return $store;
}
}
?>
<?php
/*
* How to USE:
* In Your PHP File (your-file.php)
* Include Class File like include('your-class-file.php');
* $Decode = new PHP_Serialize();
* Use Function Like This $_POST = $Decode->Decode_Serialize($_POST['Query_String']);
*/
/*
* Demo HERE
* you-file.php
*/
include('your-class-file.php');
$Decode = new PHP_Serialize();
$_POST = $Decode->Decode_Serialize($_POST['Query_String']);
/* Decode jQuery Serlize String AS a PHP Function if you not making Class file */
$_POST = Decode_Serialize($_POST['Query_String']);
/* Use POST String in your mySQL Statement Like This*/
mysql_query("SELECT * FROM Table WHERE COMPANY_NAME = '".$_POST['COMPANY_NAME']."'");
/*
* Here is Your form PHP File
*/
?>
<form action="" method="post" id="your-form-id" name="your-form-id">
<label for="COMPANY_NAME">Company Name:</label>
<input type="text" name="COMPANY_NAME" id="COMPANY_NAME"/>
<label for="COMPANY_EMAIL">Company Email:</label>
<input type="text" name="COMPANY_EMAIL" id="COMPANY_EMAIL"/>
<input type="button" name="submit" id="submit" value="submit" />
</form>
<script>
$(document).ready(function() {
$('#submit').click(function(){
var Post_String = $('#your-form-id').serialize();
$.ajax({
type : 'POST',
url : 'your-file.php',
data : {Query_String:Post_String},
success : function(return_value){/* On Success you jQuery Code Here*/}
});
});
});
</script>
精彩评论