Peyman Mohamadpour
Peyman Mohamadpour

Reputation: 17964

JavaScript form submits, but post data not available in PHP

It seems the form submits (to the same page - contact.php), but I can not use posted data, for example $_POST["message"] . seems they are empty (I tried to echo them and nothing printed out).

This is JavaScript (in head section of page):

$(document).ready(function (){
    $('#contactform').submit(function(){
        var action = $(this).attr('action');
        $.post("contact.php", {
            name: $('#name').val(),
            email: $('#email').val(),
            company: $('#company').val(),
            subject: $('#subject').val(),
            message: $('#message').val()
        }, function(data,status){
            alert("status = " + status);            
            $('#contactform #submit').attr('disabled','');
            if(data=='Message sent!') $('#contactform').slideUp();
           });
           return false;
    });
});  

this is form:

<form action="contact.php" method="post" id="contactform">
  <ol>
     <li>
        <label for="name">First Name <a href="#">*</a></label>
        <input name="name" id="name" class="text">
     </li>
     <li>
        <label for="email">Your e-mail <a href="#">*</a></label>
        <input id="email" name="email" class="text">
     </li>
     <li>
        <label for="company">Company Name</label>
        <input id="company" name="company" class="text">
     </li>
     <li>
        <label for="subject">Subject<br>
        </label>
        <input id="subject" name="subject" class="text">
     </li>
     <li>
        <label for="message">Message <a href="#">*</a></label>
        <textarea id="message" name="message" rows="6" cols="50"></textarea>
     </li>
     <li class="buttons">
        <input type="image" name="submitbtn" id="submitbtn" src="images/but_send_message.gif">
     </li>
  </ol>
</form>  

The alert("status = " + status); section on javascript pops up the status as sucess.

UPDATED

And this is PHP part at the top of contact.php

<?php
  if(isset($_POST["message"])){
    echo '<script>alert("some dummy text");</script>';
  };
?>  

It is not just that echo does not print anything, but I can not access values from posted data. PHPMailer works fine with manually assigned text to parameters.

Upvotes: 2

Views: 2250

Answers (5)

karthi
karthi

Reputation: 887

Save your post values in a variable. For example:

$name = $_POST["name"];

You can echo this variable in your script:

var name ="<?php echo $name; ?>";

Upvotes: 0

Peyman Mohamadpour
Peyman Mohamadpour

Reputation: 17964

as Sheikh heera mentioned in his comment on my question, I tried this:

$(document).ready(function (){
    $('#contactform').serialize(function(){
        var action = $(this).attr('action');
        $.post("contact.php", {
            name: $('#name').val(),
            email: $('#email').val(),
            company: $('#company').val(),
            subject: $('#subject').val(),
            message: $('#message').val()
        }, function(data,status){
                $('#contactform #submit').attr('disabled','');
                if(data=='Message sent!') $('#contactform').slideUp();
          });
          return false;
    });
});

and it works fine now! thanks to other users that suggested alternate solutions which may be working on this case but as I found the solution, there is no need to check them.

Upvotes: 0

Rachit Doshi
Rachit Doshi

Reputation: 183

Do the Following: 1) provide an id or a class to the li class button's child's input tag

2) Then in jquery write a code to handle :

$('.inputButton').click(function(){
    var data = $('#contactform').serializeArray();
    $.each(data, function(key, field) {
        // Perform your validations over the data here
        console.log('field Name == '+field.name+', field value == '+field.value);
    });


    // Make an ajax call using this data
    $.post("contact.php", data, function(returnData) {
        // Handle your code for after form submission response
    });
});

3) Then in PHP you can get values as :

$name = $_POST["name"]; // and so on...

Hope this helps you solve your problem

Upvotes: 0

Rajendra Khabiya
Rajendra Khabiya

Reputation: 2030

Just try this code to post the form and check will getting $_POST on contact.php or not

<script type="text/javascript">

$(document).ready(function (){

    $("#submitbtn").click(function(){
        $("#contactform").attr("action" , "contact.php");
        $("#contactform").submit();


});

</script>

If in contact.php if you get $_POST then show success message

<?php
  if(isset($_POST["message"])){
    echo '<script>alert("some dummy text");</script>';
  };
?> 

Upvotes: 1

Gokul Gopala Krishnan
Gokul Gopala Krishnan

Reputation: 323

If $_POST returns empty data. Make sure that you don't have any htaccess causing this. I had this problem once. My htaccess always emptied the post data. After modifying the htaccess I got my problem solved.

Upvotes: 1

Related Questions