how can i get values from serialized form? - php

i have this block of code. What I want is to get the values from form control formVal. How can I get those values like key-value pair?
alert(jQuery(".dateMasking").serialize());
var formVal = jQuery(".dateMasking").serialize();
alert(formVal['waitlist_opens']);
The first alert shows as: waitlist_opens=2015-03-10, but the second alert doesn't show as expected: 2015-03-10

Got it! Thanks! :)
alert(jQuery(".dateMasking").serializeArray());
var formVal = jQuery(".dateMasking").serializeArray();
jQuery.each( formVal, function( i, field ){
alert(field.value);
});

alter your code with the following to get the date
alert(jQuery(".dateMasking").serialize());
var formVal = jQuery(".dateMasking").serialize();
formVal = formVal.split("=");
for(i=0; i<formVal.length; i++) {
alert(formVal[i]);
}

Related

JSON syntax with AJAX call and jquery each loop

I have an AJAX call that returns the data from PHP
echo json_encode( $json_response);
and the result is as follows
[{"name":"Sprouts.......}]
I then loop through it with JQUERY
$.each($.parseJSON(data), function(i, obj) {
var name = (obj.name);
var address = (obj.address);
});
This works perfectly fine, but I am trying to add a name (child) to it so that I can have other children. I used the following
json_encode(array('storedata' => $json_response))
to add the name storedata which returns
{"storedata":[{"name":"Sprouts....
I have tried looping through with
$.each($.parseJSON(data.storedata), function(i, obj) {
var name = (obj.name);
var address = (obj.address);
});
but something is wrong with the syntax because the data.storedata is undefined.
What is the correct syntax and/or correct way to do this. Thank you for any and all help.
The problem is that you are trying to access storedata before parsing the string into an object, so you need to replace $.parseJSON(data.storedata) with $.parseJSON(data).storedata:
$.each($.parseJSON(data).storedata, function(i, obj) {
var name = (obj.name);
var address = (obj.address);
});

Print an element array in jquery

I have a jquery function that calls a controller which in turn calls a function that I extracted the data from the users table. I return an array cin all the data in the table. ID, username etc etc.. So mold date in jQuery and indeed the array is full. Ex. [{"Id_user": "21", "username": "cassy1994"}].
From this array I have to extract only the username. How can I do?
This is the Ajax function:
$(".apprezzamenti").click(function()
{
var id = $(this).attr('id');
var txt = "";
$.get("http://localhost/laravel/public/index.php/apprezzamenti/"+id,
function(data)
{
$(".modal-body-apprezzamenti>p").html(data);
});
});
If the json is parsed, your object is stored in an array, so:
data[0].username
If not (if it is a string), you need to parse it first:
data_parsed = JSON.parse(data);
// data_parsed[0].username
assuming data is where your response is stored, data[0].username should work. Example:
$(".modal-body-apprezzamenti>p").html(data[0].username);
To print all usernames:
var count = 0;
$.each($(".modal-body-apprezzamenti>p"), function() {
this.html(data[count].username);
count++;
]);
Hope this helps!
So, this is the problem. It's okay though, I realized that with this code I'm using can not seem to generate as many sections as there are users want to print and it shows me only the last username because it can generate only a paragraph in html. A possible solution which would it be?
This is the code:
$(".apprezzamenti").click(function()
{
var id = $(this).attr('id');
var txt = "";
$.get("http://localhost/laravel/public/index.php/apprezzamenti/"+id,
function(data)
{
data_parsed = JSON.parse(data);
for(var i=0; i<data_parsed.length; i++)
{
$(".modal-body-apprezzamenti>p").html((data_parsed[i].username));
}
});
});

Php variable to JavaScript

How to add the value of php variabele to jquery variable and adds them,i have a form that will take a value and post to second page where second page contains a div panel where the form checkboxes values are adding and subtracting,basically i want to add the submitted value from page1 to page2.Here is the code.I have 3 form values which will be redirected one by one.Whenever user submits the respective button
if($_POST['submit'])
{
$beg=$_POST['basic'];
}
function refreshPrices() {
var currentTotalValue = 0;
var beg=?????
$("#results div").each(function() {
if (!isNaN(parseInt($(this).find("span").text().substring(1)))) {
currentTotalValue += parseInt($(this).find("span").text().substring(1));
}
});
$("#totalValue").text("$" + currentTotalValue)
}
var beg=<?php echo $beg; ?>
Try this:
var beg = '<?php echo $beg ;?>';
if you want to add to the total value you can do this:
currentTotalValue = currentTotalValue + parseInt(beg);
its better to do it this way :
var js_var = '<?=json_encode($php_var);?>';
This way you will be able to transfer any type of data to js (arrays, objects, strings, etc.) and access it easily like this:
js_var['element']
var beg = '<?php echo $beg ;?>';
But this can be only one once during the load of the page. After that we cant assign a server side script to js.
Retrieve value by input class or id like dat
var beg = $("#basic").val();

jQuery Splitting of 2 values

I am trying to split the values of an array i'm generating through an autosuggest.
The output of the values are as such:
[
{"value":"12","name":"Solid Fuel Fire Installers"},
{"value":"11","name":"Oil Engineers & Boiler Fitters"}
]
I want to extract the values 12 & 11 and store as a variable so I can save these to a database through php. This is what I have tried so far and to no success:
<script type="text/javascript">
var arr = $(".as-values").val().split(",");
var category_1=arr[0];
var category_2=arr[1];
var category_3=arr[2];
</script>
I only have 3 categories as I have set a limit on the amount they can add (3).
Many thanks in advance.
That looks like json. Why not just access it as a native JS structure?
var json = $(".as-values").val();
data = jquery.parseJSON(json)
alert(data[0].name); // solid fuel fire installers
The array is an array of objects, not strings, so why not treat them as such?
<script type="text/javascript">
var arr = $(".as-values").val();
var category_1=arr[0].value;
var category_2=arr[1].value;
var category_3=arr[2].value;
</script>
Not sure what you're trying to do. Does this sound like it?
var values = '[{"value":"12","name":"Solid Fuel Fire Installers"},{"value":"11","name":"Oil Engineers & Boiler Fitters"}]';
values = $.parseJSON(values);
var submitValues = {};
$.each( values, callback(i, element){
submitValues[element.value] = element.name;
});
The string you're trying to use is valid json so you should be able to just decode the string with native functions in js that would be JSON.parse(json_string, reviver) in php you would use json_decode($json).
Decoding the string will give you an object/array you should be able to work with easily.
(This is what you want right?)
Is your question how to access an element by value of its property? You could do this:
var elements = [
{"value":"12","name":"Solid Fuel Fire Installers"},
{"value":"11","name":"Oil Engineers & Boiler Fitters"}
]; // Retrieve however you want
var element;
for(var i = 0; element = elements[i]; i++) {
if(element.value == 11) {
// It's number 11, for example
}
}
Of course you should probably put that in a function:
function findByProperty(elements, property, value) {
var i, element;
for(i = 0; element = elements[i]; i++) {
if(element[property] === value) return element;
}
return null;
}
// ... define 'elements' as before
var numberEleven = findByProperty(elements, 'value', "11");
Since you already have an array of a valid json you dont need to parse it. Just try this.
var arr = [
{"value":"12","name":"Solid Fuel Fire Installers"},
{"value":"11","name":"Oil Engineers & Boiler Fitters"}
]
var category_1=arr[0].name;
var category_2=arr[1].name;

how to extract data from an array in PHP

Hello Stack Overflow i hope you are well today;
So i am using jQuery to append a form with more input fields;
The Session is storing the data as an Array for the key: Children.
I would like to know show the user the data in that array how would this be done?
jQuery that adds the input fields (some pages previous)
$('.children-main-add').click(function(){
var attrName = $(this).attr('name');
var count = $(this).attr('count');
$(this).attr('count', (parseInt(count)+1))
var input = $('<input />');
input.attr('type','text')
input.attr('name',attrName+"["+count+"]" )
$('.children-main').append($('<li />').append(input));
$('#content li').removeClass('alt');
$('#content li:odd').addClass('alt');
$(this).val('Add Another Child');
})
The data from the session : Key: children / Value: Array
If i was not clear on something please let me know !
I thank you in advance for your help
if you want to show them as an continue string
use impode(',' , $_SESSION['children']);
Or
if(isset($_SESSION['children']) && is_array($_SESSION['children']))
{
foreach($_SESSION['children'] as $child)
{
echo $child."<br />";
// some other html
}
}

Categories