JQuery Getting PHP $POST Data?

Fait

Seasoned Veteran
Joined
Oct 15, 2010
Messages
4,407
Reaction score
561
FP$
2,054
Hello,

I am wondering how do you get the $_POST data with jquery?

I am using these functions, it does get the get the page data but won't output any post data with PHP

.load

Code:
$(document).ready(function() {

$('#submit').click(function() {



$('#content').load('ajax/check.php');



});

});



$ajax 



[code]


$(document).ready(function() {

$('#submit').click(function() {



$.ajax({ 

	url: 'ajax/chat.php',
	type: 'post',
	data: {method: 'fetch' },
	success: function(data) {
		$('.chat .messages').html(data);
	
	}
	
	
	});











});

});


I have ask before but since I have been away from the computer due to some issues with some systems previously I need some help in revising so I can get back up to where I was, Looked on google I can't find much.

I have just started with JQuery A few months ago But like I said due to some issues I need help in revising.

Thanks,

Spudster
 
$_POST is an array in PHP which has the post request values, like from a form with method="post".... It is a server-side data structure and you can't plainly access it client side using javascript... You can create a script to echo(not the array a specific index) or serialize or json the post array and use jquery .load to invoke it... But whats the point in using the data available on client side from server?
 
Im trying to make A login without the page refreshing.

I have made A chat of A tutorial but don't get how he did it with A client side script to connect to server side like php.

Now i'm trying to achieve a login.

I think it is .val(); but Working out what function goes with it.
 
To make a login without page refreshing :
Your login page :-
Code:
<html>  
<head>                
      <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
</head>
<body>
      <div id="content"></div>
<form action="" method="post" name="login-form" id="login-form"> 
  <input type='text' name='user_login' id='user_login' placeholder='eg: [email protected]'> <br>
         <input type='password' name='pass_login'> <br>
         <input type='submit' name='submit_form' id='submit' value='Login'> 
</form>

 <script type="text/javascript">
        $(document).ready(function(){
            $('#login-form').click(function() {
$.ajax({
        url: 'some-url',
        type: 'post',
        dataType: 'text',
        data: $(this).serialize(),
        success: function(data) {
                 if (data == 'true')
{
alert('login success');
}
else if (data == 'false')
{
alert('login failed');
}
else
{
alert('some error!');
}
                 }
    });
                return false;
            });
        });
    </script>
</body>
         </html>

On server side :
Code:
 <?php 
if($_POST)
{
// Do the check from data in DB and if true echo 'true';
// else echo 'false';
}
else
{
echo 'false';
}

Additionally you can add validations like if proper input is given and whether input is empty etc etc... This is just a basic example....

I hope it helps 🙂
 
Think there is an error in your script somewhere.

Added the server and client side. And I get 'Some error' I go to the network tap and it appears to be hitting th document.

Thanks for the help so far :.)
 
Ok I give it A try tommorow when I get on my PC.

Thanks.<br /><br />-- May 25th, 2013, 12:16 pm --<br /><br />Thanks :.)

I manage to doit via A tutorial from PHPAcademy.

Now I understand the basics of connecting JQuery with PHP.

Heres what I have now

Code:
$(document).ready(function() {


$('#submit').click(function() {

		var user_login = $('#user_login').val();
		var password = $('#pass_login').val();
		

$.post('ajax/check.php', {user_login: user_login, password: password}, function(data) { 
$('#content').html(data);

} );


});








});


Now I want it to process the data if the user also hits enter or clicks the submit button, How could this be achieved?

Thanks
 
Back
Top Bottom