PHP Serialize Failed & stored in semi-serialize format - javascript

When i add new category, i am storing child category in serialize format, Which works just perfect. Like :-
a:1:{i:0;s:2:"41";} But when i edit it back and try to change it to other category, which are loaded via Ajax-Json Javascript. But value is still 41 as above. It stores value in below format :-
s:2:"41";, Which is wrong and i get error while going back and editing it again.
Does any one know how this serialize failed to store in proper format ?,
Thanka

I got it Working, Oh, I had to dig and Understand what is serialize first and how php understands and converts it in serialize format.
Here you go from php.net
String s:size:value;
Integer i:value;
Boolean b:value; (does not store "true" or "false", does store '1'
or '0')
Null N;
Array a:size:{key definition;value definition;(repeated per
element)}
Object O:strlen(object name):object name:object
size:{s:strlen(property name):property name:property
definition;(repeated per property)}
That means, I was not sending value as array, and since value was not getting sent in array via HTML form, it didn't appended a for array tag. :)
And when i made my HTML``input field name as category[] from category, Everything worked as expected :)
Thanks

How are you editing it (code sample)?
You should unserialize it first, then modify, and serialize again.

Related

JSON.stringfy return data different compared with the object

I have some code:
console.log("444444: ", profile, JSON.stringify(profile))
when I check the log:
Basically I want to know why I can not see
value: [0]
and i want to pass this profile object to backend and looks like backend is not getting this value field.
Probably, you remove profile.value somewhere after you outputted it to console.log(). Try looking for delete profile.value; or profile.value = undefined;.
Try to parse the data first and then stringify
JSON.parse(JSON.stringify(profile))

Getting string index value from Javascript to MVC C# Controller using string[] array as a parameter

I am new in this environment and i just want to ask somebody if it is possible to get the string value not the index value using string[] array as a parameter?
Below are those images: ajax pass this data into controller
I am using ajax to pass my data in to the url controller in c# mvc.
By the way, here's my sample array data: prepared data..
the highlighted one is my array and in my parameter in mvc # is declared as string [] methodParam: highlighted parameter,
Those not highlighted parameter are working. all i want to do is getting one by one string in methodParam. i tried to use this
GetMethodParam.IndexOf("Date").ToString() but the output is -1 which probably not available in context.
i just want to get each string and its value because i send it to the email outlook..
like this. enter image description here.
Any Suggestions, clarification or comments is highly appreciated. Thank you ;) .
You don't need the individual input in data on ajax call, just use the last one UserHeader
and in Acton(C#) use a model as argument with containing all attributes of UserHeader.
Or, remove UserHeader from data and Use each attributes as individual argument in C# Action with same name.

Easiest way to insert a JS array into MySQL using PHP

UPDATE!! My code is actually valid to run. The original question is below. If you are still curious please read the question and then back to read my update.
My bad, the db actually can get updated successfully after the code runs. I accidentally set the datatype in the table as INT but my initial intention, varchar.
But from HenryDev's answer I managed to find the acquired array by post method in php becomes STRING type but array type. I used
$cameArray = $_POST['cameArray'];
echo gettype($cameArray);
to output "string" on my screen, so implode would not work oh that(see the discussions on HenryDev's answer).
However his answer will work as a charm if you set up an array in PHP and implode it, it will give you back a "string type" of your array!
UPDATE!! My code is actually valid to run. The original question is below. If you are still curious please read the question and then back to read my update.
Here is my original question
So I have a very basic question but the tutorials and answers I found online are kinda complicated for my situation.
I have a JS array like
var sentArray = ['1','2','3'];
I want to insert this array into the database as one field of a table. I use ajax to send this array to PHP and then execute the query in PHP. For instance,
$cameArray = $_POST['cameArray']; //then $cameArray is sentArray
mysqli_query($con, "INSERT INTO thisTable (arrayField) VALUES ($cameArray)");
And then I want the value in the database shown as "1, 2, 3", but the problem is the value in the database is simply shown as a single "2". The type in table is varchar.
No any JSON object involved just pure text. How could I do that? Thanks!
Here's a quick solution. If you want to store the array as an string simply do this in your PHP code.
$array = array('1', '2', '3'); // Your array
$cameArray = implode(",", $array);
echo $cameArray; // "1,2,3"
Now you can store the variable $cameArray in your Data Base. Hope it helps!
My bad, the db actually can get updated successfully after the code runs. I accidentally set the datatype in the table as INT but my initial intention, varchar.
But from HenryDev's answer I managed to find the acquired array by post method in php becomes STRING type but array type. I used
$cameArray = $_POST['cameArray'];
echo gettype($cameArray);
to output "string" on my screen, so implode would not work oh that(see the discussions on HenryDev's answer).
However his answer will work as a charm if you set up an array in PHP and implode it, it will give you back a "string type" of your array!

How to delete an attribute when sending a partial object update to Algolia?

I need to use partialUpdateObject from the Algolia Javascript SDK to delete an attribute from an object that is already in the index.
My scenario is that I have a description field on my object that is optional. If the user originally sets a description and then later deletes it, I want to remove the description altogether from the object.
I do not want to overwrite the whole object (without the description attribute) because I have some count attributes on the object that I do not want to have to recalculate.
As far as I can tell in the documentation there isn't a way to do it and my workaround is to set the description as an empty string. Is that the recommended approach?
You're right: you cannot totally remove the attribute from an object with a partialUpdateObject operation. As a work-around you can set it to null, maybe that's enough for your use-case?
If you really want to delete the field you can :
Get your object with the search function
Store all fields values
Update (not partial update) your object without passing the field you want to delete

pass multidimensional javascript array to another page

I have a multidimensional array that is something like this
[0]string
[1]-->[0]string,[1]string,[2]string
[2]string
[3]string
[4]-->[0]string,[1]string,[2]string[3]string,[4]string,[5]INFO
(I hope that makes sense)
where [1] and [4] are themselves arrays which I could access INFO like myArray[4][5].
The length of the nested arrays ([1] and [4]) can varry.
I use this method to store, calculate, and distribute data across a pretty complicated form.
Not all the data thats storred in the array makes it to an input field so its not all sent to the next page when the form's post method is called.
I would like to access the array the same way on the next page as I do on the first.
Thoughts:
Method 1:
I figure I could load all the data into hidden fields, post everything, then get those values on the second page and load themm all back into an array but that would require over a hundred hidden fields.
Method 2:
I suppose I could also use .join() to concatenate the whole array into one string, load that into one input, post it , and use .split(",") to break it back up. But If I do that im not sure how to handel the multidimensional asspect of it so that I still would be able to access INFO like myArray[4][5] on page 2.
I will be accessing the arrary with Javascript, the values that DO make it to inputs on page 1 will be accessed using php on page 2.
My question is is there a better way to acomplish what I need or how can I set up the Method 2 metioned above?
This solved my problem:
var str = JSON.stringify(fullInfoArray);
sessionStorage.fullInfoArray = str;
var newArr = JSON.parse(sessionStorage.fullInfoArray);
alert(newArr[0][2][1]);
If possible, you can use sessionStorage to store the string representation of your objects using JSON.stringify():
// store value
sessionStorage.setItem('myvalue', JSON.stringify(myObject));
// retrieve value
var myObject = JSON.parse(sessionStorage.getItem('myvalue'));
Note that sessionStorage has an upper limit to how much can be stored; I believe it's about 2.5MB, so you shouldn't hit it easily.
Keep the data in your PHP Session and whenever you submit forms update the data in session.
Every page you generate can be generated using this data.
OR
If uou are using a modern browser, make use of HTML5 localStorage.
OR
You can do continue with what you are doing :)

Categories

Resources