nancy harigton
nancy harigton

Reputation: 13

value is not coming $POST array in php

I am building a simple sign up form using ajax when I creating a data object and pass to PHP file.It shows variables and doesn't show values of that PHP variable. The code of HTML of form is

<form id="myForm" name="myForm" action="" method="POST" class="register">
    <p>
        <label>Name *</label>
        <input name="name" type="text" class="long"/>
    </p>
    <p>
        <label>Institute Name *</label>
        <input name="iname" type="text" maxlength="10"/>
    </p>         

    <div>
        <button id="button" class="button" name="register">Register &raquo;</button>
    </div>
</form>

The code of js is

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script>
$(document).ready(function(){
    var form=$("#myForm").serialize();
    $("#button").click(function(){
        $.ajax({
            type:"POST",
            url: "mainlogic.php",
            data:form, 
            success: function(result){
                alert(result);
            }
        });   
    });
})
</script>

The code of PHP is (mainlogic.php)

    if(isset($_POST)) {
         print_r($_POST);//////varaibles having null values if it is set
         $name=trim($_POST['name']);
         echo $name;        
    }

Upvotes: 1

Views: 678

Answers (5)

Takit Isy
Takit Isy

Reputation: 10081

Alpadev got the right answer, but here are a few leads that can help you in the future:

ajax

You should add the below error coding in your Ajax call, to display information if the request got a problem:

$.ajax({
    […]
    error: function(jqXHR, textStatus, errorThrown){
        // Error handling
        console.log(form); // where “form” is your variable
        console.log(jqXHR);
        console.log(textStatus);
        console.log(errorThrown);
    }
});

$_POST

$_POST refers to all the variables that are passed by the page to the server. You need to use a variable name to access it in your php.

See there for details about $_POST:
http://php.net/manual/en/reserved.variables.post.php

print_r($_POST); should output the array of all the posted variables on your page.
Make sure that:
⋅ The Ajax request ended correctly,
⋅ The print_r instruction is not conditioned by something else that evaluates to false,
⋅ The array is displayed in the page, not hidden by other elements. (You could take a look at the html source code instead of the output page to be sure about it.)

Upvotes: 0

Kancho Iliev
Kancho Iliev

Reputation: 701

The problem is that you calculate the form values at the beginning when loading the page when they have no value yet. You have to move the variable form calculation inside the button binding.

<script>
$(document).ready(function(){
    $("#button").click(function(){
        var form=$("#myForm").serialize();
        $.ajax({
            type:"POST",
            url: "mainlogic.php",
            data:form, 
            success: function(result){
                alert(result);
            }
        });   
    });
})
</script>

Upvotes: 0

SlaWitDev
SlaWitDev

Reputation: 467

In this code you serialize blank form, just after document is ready:

<script>
$(document).ready(function(){
    var form=$("#myForm").serialize();
    $("#button").click(function(){
        $.ajax({
            type:"POST",
            url: "mainlogic.php",
            data:form, 
            success: function(result){
                alert(result);
            }
        });
    });
})
</script>

Valid click function should begins like:

$("#button").click(function(){
    var form=$("#myForm").serialize();
    $.ajax({...

It means - serialize form right after button clicked.

Upvotes: 1

Quentin
Quentin

Reputation: 943142

var form = $("#myForm").serialize();

That is the line that collects the data from the form.

You have it immediately after $(document).ready(function() { so you will collect the data as soon as the DOM is ready. This won't work because it is before the user has had a chance to fill in the form.

You need to collect the data from the form when the button is clicked. Move that line inside the click event handler function.

Upvotes: 0

alpadev
alpadev

Reputation: 480

You are serializing your form on document load. At this stage, the form isn't filled yet. You should serialize your form inside your button click event handler instead.

$(document).ready(function(){

    $("#button").click(function(){
        var form=$("#myForm").serialize();
        $.ajax({
            type:"POST",
            url: "mainlogic.php",
            data:form, 
            success: function(result){
                alert(result);
            }
        });   
    });
})

Upvotes: 3

Related Questions