save url in MySql databse via PHP getting failed - php

I have a array of URL which I am saving in database after iteration on PHP side.
I am sending the array with Ajax and saving it with PHP.
Data send via Ajax
linksString=http://localhost/phpmyadmin/index.php?db=testdb&token=42d0dde57469a9aa4b6a2f7e0741,
http://localhost/phpmyadmin/index.php?db=testdb&token=98604a9aa4b6a2f7e0741,
http://localhost/phpmyadmin/index.php?db=testdb&token=9864dde57469a9aa4b6a2f7e0741,
http://localhost/phpmyadmin/index.php?db=testdb&token=986042d0dde57469a9aa4b6a2f7e0741,
http://localhost/phpmyadmin/index.php?db=testdb&token=986042d0dde57469a9a23&q=save
Not getting all values in $linksPieces , Getting only one value
But I am not getting this all string in PHP side; only getting first sub string which is before first comma(,).
I.E
http://localhost/phpmyadmin/index.php?db=testdb&token=42d0dde57469a9aa4b6a2f7e0741
PHP
$linksPieces = array();
$links = $_POST['linksString'];
$linksPieces = explode(",", $links);
foreach($linksPieces as $link)
{
//operation
}
I need to get all the string in array on PHP side.
If I am sending these type of url which have not any = then working fine.
http://in.yahoo.com/
http://www.hotmail.com/
http://www.google.com/
http://www.blah.com/
http://www.blah1.com/

You can try using encodeURIComponent() in Javascript before sending this string and using urldecode() in PHP before using this string.

Related

Strange value when using Base64enconde and decode

I m building an website and i'm using the base64encode() and base64decode().
I send the base64encode(value) through and url and i receive and then i decode the value. On the page that i have the decoded value i have one button that goes to the previous page and i use the encode to encode again the value and send it. the previous page i decode that value and i encode again to send to the same page.
At the first time i receive the correct values but when i do more than 1 i get strange value. It is my code:
The first page:
Here i receive one value and i decode it.
$tablename = base64_decode($_GET['tablename']);
In the end of the page i have on list with this code, here i send the encoded value:
$descricao = base64_encode($row["descricao"]);
$tablename = base64_encode($tablename);
?>
<a href="./gestaoalarme.php?descricao=<?=$descricao?>&tablename=<?=$tablename?>">
In the seconde page i receive the encoded value
$tablename=base64_decode($_GET['tablename']);
and i have that button that goes back and send the encode value.
<a href="./verdispositivos.php?tablename=<?=base64_encode($tablename)?>" class="btn Back btn-lg">
The first time results, but then not.
Base64 encoded values are not URL safe due to the + / and = characters.
You will also need to use urlencode() before using the string in a url.
E.g.
$value_to_encode = 'some=string/here+foobar';
$encoded = urlencode(base64_encode($value_to_encode));
$decoded = base64_decode(urldecode($encoded));

preg_replace and json_decode - Ajax handler - php

I have problem with array in PHP.
I've got that code in my Ajax handler PHP page:
if(isset($_GET['dataa']))
{
$dataa = $_GET['dataa'];
$dataa = preg_replace('/\\\"(.*?)\\\"/', '"$1"', $dataa);
$dataa = json_decode($dataa);
echo $dataa[0];
echo $dataa[1];
}
I tried to send data array in JavaScript to the Ajax handler but it's not working so I want to to pass param in the URL.
When I try to put this URL:
...AjaxHandler.php?dataa=["test1","test2"]
I get this: test1test2 and that's ok.
But I don't really understand why I need to use preg_replace and json_decode.
I tried to delete the preg_replace but then it didn't show me anything.
I tried to delete the json_decode too but then I get only [/ on the screen.
I need to send multiple data so I do that with array.
Why I have to use preg_replace?
Is it common to use preg_replace like that? All time when I send data array?

PHP json decode breaking when image tag present

Here is my json encoded string coming from a db query:
[{"columns":[{"class":"grid","html":"<p></p><img src="http://localhost/media/image.jpg"><p></p>"}]},{"columns":[{"class":"grid","html":""}]}]
When I go to json_decode in PHP I am getting NULL.
$content = json_decode($json_str); = NULL
I haven't had this issue before. I am using JSON.stringify when saving the item into the db via ajax like so:
var data = JSON.stringify(html);
Do I have to start treating this differently for some reason?

zend php multiple array json_encoding

I have a problem with the json functionality in zend and js.
I try to encode a single array containing some number of models like this:
echo json_encode(Application_Model_Marker::getMarkers());
var mark = JSON.parse(jsonVal); //in js
where getMarkers is a static method that returns an array of marker models.
This works fine and when I parse it in the js script and try accessing the values of the json object it works fine.
If however I try to create and send an array of array like this:
$allData = array();
$allData['info'] = Application_Model_Marker::getMarkers();
$allData['openingHours'] = Application_Model_Openinghours::getOpeningHours();
$allData['happyHours'] = Application_Model_Happyhour::getHappyHours();
echo json_encode($allData);
It still sends all the correct information when I try to alert(jsonVal.responseText); in js.
It has three arrays each containg some arrays of objects.
However when I try to initialize a variable to the parsed json object like in the first example, I can't access the values and it seems some kind of error occurs as the program stops when I try it.
I don't quite get it as it has all the correct info when i just try to print the response text from the encoded json object.
Any ideas how to do this multidimensional json encoding?
Try this, Hopefully it'll work :
<sctript>
var mark;
eval("mark = "+jsonVal+";");
</sctrip>

Sending an array of data to the server with jQuery

I am sending a CSV list to the server within the url. It is a list of songid's:
var $array = [];
oTable.find('td').each(function(){
$songdata = $(this).data('data');
$songid = $songdata.songid;
//The next line is the variable I need to also send along with each songid
$duration = $songdata.duration;
$array.push($songid)
});
This results in '24,25,32,48,87' being sent to the server.
I then use the following in my php script to convert it into an array:
$songs = $_GET['songid'];
$songarray = explode(",", $songs);
This works great, I can access the data properly and all is well.
However I now have the need to add another property to each of the songid's. As seen above, I need to add the $duration variable to the data sent and be able to convert it into an array server side. (or alternatively construct the actual array client side would be fine also)
How would I go about this?
You could use this to encode your array into JSON:
http://www.openjs.com/scripts/data/json_encode.php
And then send that String to your backend where you can unpack it with this:
$yourarray = json_decode($string)
I would create an object of the data in your jscript and send it over to the PHP and then use json_decode then you have an associative array of your data within the PHP
You always can use JSON for data serialization/deserealization as of Serializing to JSON in jQuery
try sending arrays to PHP in JSON format and then do json_decode($var) in your PHP script.
make an associative array server side
$array = new Array();
//add songid as key and duration as value
$array = array_push_assoc($array, $songid, $songduration);
echo json_encode($array);
on the client side after parsing the json
$.each(json,function(key,value){
//key will be the songid
//value will be the duration
});

Categories