I have a form which I submit and I use .serialize to help me gather all data from the form. Now, I’ve checked in firebug what is sent in POST and I can see a nice formated string like example
index.php?data=sth&data2=sth
but the problem is with this & in the PHP – the PHP outputs it like this:
index.php?data=sth& a m p ;data2=sth
See this amp? Well it is written without spaces and it’s and encoded (as I found out in google) version of &. So, what should I do to input this url correctly in the database and then fetch it a nd show it on the site without this & amp; ?
edit: if it’s possible I would like a string with that & to be put in that format to DB. (so, with & sign).
edit#2:
how I send data:
var formData = $("#myForm :input[value]").serialize();
$.ajax({
type: 'POST',
cache:false,
url: '_ajax/updateGameInfo.php',
async: false,
dataType: 'text',
data: allData,
success: function(jsonObj) {
if (jsonObj){
$msg = 'Data sucessfully updated! Reloading page...';
alert($msg);
}
else{
$msg = 'Error with the update!';
alert($msg);
}
}
});
And here is my updateGameInfo.php:
$gameRepos = new GameRepository();
$game = $gameRepos->updateGame();
echo json_encode( $game );
And if you also like, here is my updateGame function from GameRepository:
public function updateGame()
{
$cleanPost = array_map( array('GameRepository', 'cleanPostData'), $_POST);
$attributes = array_keys($cleanPost);
$values = array_values($cleanPost);
$table = $this->resolveTableName( $cleanPost["selectedTypeId"] );
$id = $cleanPost["selectedGameId"];
if ($this->openConnection())
{
$pairs = "";
foreach ($cleanPost as $attribute => $value){
if ($attribute != "selectedGameId" && $attribute != "selectedTypeId"){
if ($attribute == "url")
$value = str_replace("amp;", "", $value);
$pairs .= $attribute . "='" . $value . "',";
}
}
$pairs = rtrim($pairs, ','); //remove last comma
$query = "UPDATE $table SET $pairs WHERE id=$id;";
$result = pg_query($query);
if (!$result){
mysql_error());
return false;
}
else
return true;
}
else
return false;
}
If you are passing in the URL as a POST variable, then PHP is going to automatically encode any special characters. So, assuming the POST variable name is
url, you would want to do this in your php script before everything else:The issue isn’t with jquery, it’s with php. You’ll run into similar issues when passing json strings.