Hello,
I have a php variable $birthday, I am showing it's value like this:

<div id= "dob" class="pick_number">Day of Birth: <?php echo $birthday ?></div>
<div id="test"></div> // value should be printed back in here.

I need to pass the value of $birthday via ajax, I had the following but it seems not working, no value is printed back in test div:

var $birthday =$('#dob'); // to select the above code

$.get("process.php", 
    {birthday: $birthday},
    function(chart) {
    $test.text(chart);
});

Thanks.

I'm not sure, but I don't think javascript likes a dollar sign at the start of a variable name (i.e. $birthday). Also, i'm assuming you're using jquery, so $test.text(chart) should be $('#test').text(chart). Try this:

var birthday = $('#dob');

$.get("process.php", 
    {birthday: birthday},
    function(chart) {
    $('#test').text(chart);
});

Hope this helps :)

Hi Thanks for your reply,
It did not like it without the $:

var birthday = $('#dob'); // it hangs

var $birthday= $('#dob'); // did not hang but did not print back anything

I tried this:

var birthdayStr = $birthday.val().replace(/[^0-9\-]+/g,''); 
$.get("process.php", 
		{birthday: birthdayStr},
		function(chart) {
				$test.text(chart);
			});

it printed empty value: birthday is:
Any ideas?

In the first line, you should use text() or html() instead of val() since it isn't a form element. If that doesn't work, you could try this:

var birthdayStr = $('#dob').html().replace(/[^0-9\-]+/g,'');
$("#test").load("process.php", { birthday: birthdayStr } );

Thanks the .html() solved it :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.