How to post a jquery variable to php

Guys,

There are multiple methods to achieve this concept. For now, i am posting method1. Have a look.

Method1:

<html>
<head>
<script src=”http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js”&gt;
</script>
<script>
$(document).ready(function(){
var id = 23;
$(“#submit”).click(function(){
$(‘#id1’).val(id);
});
});
</script>
</head>
<body>
<form action=”b.php” method=”post” >
<input type=”submit” id=”submit” name=”submit”/>
<input type=”hidden” name=”id1″ id=”id1″ value=””/>
</form>
</body>

b.php

<?php
$a =isset($_POST[‘id1’])?$_POST[‘id1′]:’not yet’;
echo $a ;
?>

Method2: Using Ajax

Will post soon..

References:

http://www.danielmayor.com/jquery-php-an-easy-way-to-pass-variables-between-php-and-javascript-with-jsonp-ajax-like

http://stackoverflow.com/questions/19462649/trying-to-pass-variable-values-from-javascript-to-php-using-ajax

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s