0

I am grabbing the values of several input fields with the help of an Ajax/JS function. The issue is that the values of the textbox are not being echoed. I checked with the firebug tool and it shows that the post is performed but there is a blank value. Why is the PHP not echoing the value when the JS function submits it?

EXAMPLE

JS

<script>
$(document).ready(function() { 
    var timer = null;  
    var dataString;    
    function submitForm(){ 
        $.ajax({ type: "POST", 
            url: "index.php", 
            dataType: 'json', 
            success: function(result){ 
            $('#special').html('<p>' +  $('#resultval', result).html() + '</p>');} 
        }); 
        return false; 
    } 
    $('#contact_form').on('keyup', function() { 
        clearTimeout(timer); 
        timer = setTimeout(submitForm, 2000); 
    }); 
});  
</script>

HTML

<form action="" method="post" enctype="multipart/form-data" id="contact_form" name="form4"> 
 <div class="row">  
  <div class="label">Contact Name *</div> <!-- end .label --> 
    <div class="input"> 
      <input type="text" id="contact_name" class="detail" name="contact_name" value="<?php echo isset($_POST['contact_name'])? $_POST['contact_name'] : ''; ?>" />  
      <div id="special"><span id="resultval"><? echo $_POST['contact_name'];  ?></span></div> 
    </div><!-- end .input--> 
 </div><!-- end .row --> 
 <div class="row">  
  <div class="label">Email Address *</div> <!-- end .label --> 
   <div class="input"> 
    <input type="text" id="email" class="detail" name="email" value="<?php echo isset($_POST['email'])? $_POST['email'] : ''; ?>" />  
    <div id="special"><span id="resultval"><? echo $_POST['email'];  ?></span></div> 
   </div><!-- end .input--> 
 </div><!-- end .row --> 
</form>
0

2 Answers 2

1

You need to use .serialize() on the form probably

Sign up to request clarification or add additional context in comments.

Comments

0

Friend first understand the Javascript behaviour. When you post a form, it becomes one request to the server. At the same time when you send an ajax to server it becomes another separate request to the server

So you should either do form post or ajax.

As you are using ajax here you, in the ajax request you have to pass data separately in data parameter

<script type="text/javascript">
$(document).ready(function() { 
    var timer = null;  
    var dataString;    
    function submitForm(){ 
        $.ajax({ type: "POST", 
            url: "index.php", 
            dataType: 'json', 
            data: $('#contact_form').serialize(), // check this line
            success: function(result){ 
            $('#special').html('<p>' +  $('#resultval', result).html() + '</p>');} 
        }); 
        return false; 
    } 
    $('#contact_form').on('keyup', function() { 
        clearTimeout(timer); 
        timer = setTimeout(submitForm, 2000); 
    }); 
});  
</script>

2 Comments

Thank you friend, I am new to this and now I am starting to understand it thanks to your help. One thing that I am still having difficulty is echoing the value of each input field: <div id="special"><span id="resultval"><? echo $_POST['contact_name']; ?></span></div>
If you want to use <? echo $_POST['contact_name']; ?> then ajax will not fill that for you, for that you will have to use without ajax. Make a simple form post and then it will work. Otherwise store $('#contact_form').serialize() data in some variable and after ajax success play with as you want. Hope you are getting

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.