简体   繁体   中英

pass data and get data into php file from html to jquery to php and then from php to jquery to html

OK i have this code

HTML:

<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js">                      </script>
<script type="text/javascript">
function get() {

$.post('tt.php',
{ 
$('#name')
},
    function(output) {
        $('#age').html(output).show();
        });

}

</script>
</head>
<body>

<input type="text" id="name" name="name"/>
<a href="javascript:get()">haller</a>
<div id="age"></div>
</body>
</html>

and then this php file.

PHP:

<?
$name=$_POST['name'];
if ($name=="juliver")
{
echo "haller";
}
elseif ($name==NULL)
{
echo "Please put a name!";
}
else
{
echo "wrong name";
}
?>

the scenario is this, whatever value that the input has, it will be pass into the php file using the jquery of course as you can see, whenever the user clicks on the anchor tag which has the get() function and then the php file process the data (execute) and then pass it back to the jquery and then the jquery will output the data into the html. as of right now, i cant make this thing work. plesae help me. thanks in advance.

Don't use functions to bind events, you can use jQuery's native click function. You must make sure, however, that you add a class name to your link and specify it as a selector:

JavaScript

$('a.class_name').on('click', function() {
   $.post('tt.php', { name: $('#name').val() }, function(data) {
      $('#age').html(data);
   });
});

PHP

if(isset($_POST['name']))
{
   $name=$_POST['name'];

   if($name == "juliver")
   {
      echo "haller";
   }
   elseif ($name==NULL)
   {
      echo "Please put a name!";
   }
   else
   {
      echo "wrong name";
   }
}
function get() {
    $.post('tt.php', { name : $('#name').val() }, function(output) {
        $('#age').html(output).show();
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM