I want to sent 2 parameters from the client ( jquery ) side to server side ( php-codeigniter ). The parameters are id ( an integer ) and text ( a string ). I tried:
window.location.href="AjaxUpdate/sendMessage/"+id+"/"+text;
which has worked for me in the past using only the id, but because the text string can have spaces I can get:
http://localhost/b1/mycontroller/myfunction/3/gdfgdgd%20 %20 gd
which gives:
An Error Was Encountered
The URI you submitted has disallowed characters.
What is the best way to prepare and transmit a string to the server side using jquery? I assume ajax will be used in some way
You can use the jQuery .ajax()
function to POST data (JSON
) to your PHP script, like this:
var postData = {
"param1" : some_value1,
"param2" : some_value2
};
console.log(postData);
$.ajax({
type: "POST",
url: "test.php",
data: postData,
success: function(){
alert(param1 + ' ' + param2);
window.open("test.php");
}
});
UPDATE:
PHP sample that I Googled and found; it doesn't seem to do much, but unfortunately I am not versed in PHP at all:
//request parameters
$param_1 = $_POST['param1'];
$param_2 = $_POST['param2'];
echo 'postData: ' . var_dump($_POST);
if ($_POST)){
echo $param_1;
echo $param_2;
} else {
echo 'problem';
}