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
}
}
Related
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]);
}
I love this jQuery datepicker that I want to implement on a PHP form with other input boxes
http://multidatespickr.sourceforge.net/
When the user hits a submit button they are brought to another page update.php where the form data is obtained via POST.
I'm looking for a line to add to the javascript so I can somehow access the array of multiple dates in the multiple datepicker via POST:
var latestMDPver = $.ui.multiDatesPicker.version;
var lastMDPupdate = '2012-03-28';
var dates = $('#simpliest-usage').multiDatesPicker('getDates');
// Version //
//$('title').append(' v' + latestMDPver);
$('.mdp-version').text('v' + latestMDPver);
$('#mdp-title').attr('title', 'last update: ' + lastMDPupdate);
// Documentation //
$('i:contains(type)').attr('title', '[Optional] accepted values are: "allowed" [default]; "disabled".');
$('i:contains(format)').attr('title', '[Optional] accepted values are: "string" [default]; "object".');
$('#how-to h4').each(function () {
var a = $(this).closest('li').attr('id');
$(this).wrap('<'+'a href="#'+a+'"></'+'a>');
});
$('#demos .demo').each(function () {
var id = $(this).find('.box').attr('id') + '-demo';
$(this).attr('id', id)
.find('h3').wrapInner('<'+'a href="#'+id+'"></'+'a>');
});
// Run Demos
$('.demo .code').each(function() {
eval($(this).attr('title','NEW: edit this code and test it!').text());
this.contentEditable = true;
}).focus(function() {
if(!$(this).next().hasClass('test'))
$(this)
.after('<button class="test">test</button>')
.next('.test').click(function() {
$(this).closest('.demo').find('.box').removeClass('hasDatepicker').empty();
eval($(this).prev().text());
$(this).remove();
});
});
});
Looking at the plugin you can store the range as a common seperated list in a regular input field. Use jquery to disable to field from direct entry so people can't "mess it up" then after you post you an implode the field on a comma and then you will have your array.
http://multidatespickr.sourceforge.net/#undefined-demo
I'm not sure if that's what you're looking for, but if you need to access the dates passed to the script via POST just do this:
$dates = explode(',', $_POST['simpliest-usage']);
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();
Here's my script, this works fine... send_array_to_other_page.html
$(function(){
//DECLARE ARRAY
var arr = new Array();
var i = 0;
$('#form').submit(function(e){
e.preventDefault();
var value = $('#box').val();
var index = arr.length;
var list = '';
//ADD VALUE TO ARRAY
arr[index] = value;
//OUTPUT VALUES IN ARRAY
for (var index in arr){
list+=index+': '+arr[index]+'<br/>';
$('#arrLength').html(arr.length);
}
//DISPLAY ARRAY
$('#display').html(list);
$('#form').get(0).reset(); //RESET FORM
});
$('#submit').click(function(){
window.location = 'send_array_to_other_page_2.php?list='+arr;
});
});
This doesn't. It outputs Array content lost. Id also like to point out the the url of this page is send_array_to_other_page_2.php. Its missing the ?list=
<?php
$arr = $_GET['list'];
echo 'The contents of the array are still... <br/>';
if(isset($arr)){
print_r($arr);
} else {
echo 'Array content lost';
}
?>
$(function(){
//DECLARE ARRAY
arr = new Array();
var i = 0;
$('#form').submit(function(e){
e.preventDefault();
var value = $('#box').val();
var index = arr.length;
var list = '';
//ADD VALUE TO ARRAY
arr[index] = value;
//OUTPUT VALUES IN ARRAY
for (var index in arr){
list+=index+': '+arr[index]+'<br/>';
$('#arrLength').html(arr.length);
}
//DISPLAY ARRAY
$('#display').html(list);
$('#form').get(0).reset(); //RESET FORM
});
$('#submit').click(function(){
window.location = 'send_array_to_other_page_2.php?list='+arr;
});
});
Try without the var arr to make it global, I don't believe the sub functions are parsing it.
Don't sent 'long' data over a URL. Most browsers have a length limit and it's very easy to exceed that and end up with corrupted data. For 'big' data, use a POST, which is not limited.
To send the array itself, just do an AJAX request and let jquery encode the array into JSON. You then handle it in PHP with json_decode() and you'll end up with a native PHP array.
Edit: Updated the JavaScript on jsfiddle based on your comment. On "submit", the array is saved at the "#form" element using the .data() method. On "click" of the "#submit" button, that data is retrieved and the url is build up.
The click event does fire before the submit event (at least in my Firefox 7), so your array is empty when concatenated to the URL string.
I put together some JavaScript on jsfiddle that might help. You do not really need to bind to the click event of the submit-button, just do the "redirect" in the submit handler function. You are building your string list there anyways. So there would no confusion what fires first, the click or the form submit event.
As for the serialization of your array, I used jQuery's .each() function but there is nothing wrong doing it by hand (if done correctly).
I could also imagine that the form is actually posted and this is why you do not see the "?list" search part of the URL.
If you don't need a complete redirect, why don't you send the data using jQuery.get()?
Ok ,
Lets say I am creating a form. And its an address form,form elements as such :
field 1 / house number
field 2 / street name
field 3 / suburb
etc etc etc
And someone fills in the form,:
1
smith street
townsville
What I want ( similar to stack overflows live form )
Is another form element, that propagates the form field entries LIVE but replaces spaces with text:
So it appears like: 1+snith+street+townsville
With a search button at the end. This then triggers the rest of the script we have already done, which basicaly grabs the lat and long of the address and displays a gmap.
Thats essentially it, but we would need that all this occurs, whilst on the form, and before submission.
Any help appreciated, have asked several other places. No joy, but always use StackOverflow..
Thanks Ozzy
$('#my_form input').change(function() {
var new_text = '';
$('#my_form input').each(function() {
new_text += $.trim($(this).val()).replace(' ', '+') + '+';
});
$('#my_display').val(new_text);
});
here something that might work. . .
$(document).ready(function(){
$('body').live('keypress', function (event) {
if ($(event.target).hasClass('form-element')) {
var text;
$('#yourform input.form-element').each(function() {
text += $(this).attr('value') + '+';
});
$('#yourtargetelementthatwillholdallthenames').attr('value', text.substring(0,text.length - 1))
}
});
});
i haven't tried this in a browser, but give it a shot. It might need some tweaking for your needs.
If you're submitting this for a search, use encodeURIComponent() instead so you encode everything needed, like this:
$("#form1 input[type=text]").bind("keyup change", function() {
var vals = $("#form1 input[type=text]").map(function() { return this.value; });
$("#otherField").val(encodeURIComponent(vals.join(" ")));
});