Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Processing Jquery Ajax form submit with php

Tags:

jquery

ajax

php

Recently I've been struggling with Jquery and Ajax while trying to submit forms with them. I have a very simple form with a username field and password field as well as a submit button. What the form is supposed to do is that once the form is submitted the info would be sent by Ajax to a php file which then adds the said form values to a database. What I am struggling with is how to get the values from Ajax to php. Here is my code:

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

var username = $('#username').val();
var password = $('#password').val();

var dataString = 'uname=' + username + '&passw=' + password;


$.ajax({

    type: "POST",
    url:'check.php',
    data: dataString,

    success: function(data){
        alert(data);//only for testing purposes
    }
});

What eludes me is how can I get the dataString from this with php?

like image 286
Tomkarho Avatar asked Feb 02 '26 07:02

Tomkarho


1 Answers

PHP file:

<?php
    print_r($_POST);
?>

jQuery part:

var dataString = 'uname=555';
$.ajax({
    type: "POST",
    url:'check.php',
    data: dataString,

    success: function(data){
        alert(data);//only for testing purposes
    }
});

brings me:

enter image description here

So my only guess would be that you are failing to fetch your data in javascript.

One more idea. Replace the type with "GET". Then in php file write a line:

echo $_SERVER["REQUEST_URI"];

What does it give you in the alert box? :)

like image 183
Andrius Naruševičius Avatar answered Feb 03 '26 23:02

Andrius Naruševičius



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!