1

I have searched the question in stackoverflow and found few answers but non of them is helped me . So I am posting it again . My php form with post method in localhost is working fine but the same code is not working in server (linux server) I dont know what the actual problem is .Please help

  1. form that i want to submit
<form class="forma" id="forma" method="POST" action="contact-form.php" name="myForm" autocomplete="off">
  <div class="formWrapper">

    <div class="group">
      <input type="text" name="name" id="name">
      <span class="highlight"></span>
      <span class="bar"></span>
      <label>Name</label>
      <p class="error-red"></p>

    </div>

    <div class="group">
      <input type="text" name="eMail" id="eMail">
      <span class="highlight"></span>
      <span class="bar"></span>
      <label>Email</label>
      <p class="error-red"></p>
    </div>

    <div class="group">
      <input type="text" name="mobileNo" id="mobileNo">
      <span class="highlight"></span>
      <span class="bar"></span>
      <label>Mobile no.</label>
      <p class="error-red"></p>
    </div>

    <div class="group">
      <input type="text" name="message" id="message">
      <span class="highlight"></span>
      <span class="bar"></span>
      <label>Message</label>

    </div>

    <div class="captchaWrapper">
      <img src="captcha.php?rand=<?php echo rand(); ?>" id="captchaimgCont" style="margin-right: 14px;" alt="captcha">


      <a href="javascript: refreshCaptcha();" class="clickTorefreshAnchor">
        <i class="fa fa-refresh clickTorefresh" id="caprefresh">

                                        </i>
      </a>

      <div class="captchaInputWrapper group">
        <input type="text" id="6_letters_code" name="captcha">
        <span class="highlight"></span>
        <span class="bar"></span>
        <label>Enter the code here:</label>
        <p class="error-red"></p>

      </div>
    </div>


    <div class="submit submitBtn" id="consubmit">
      <button type="submit" name="sendmail" id="sendmail" class="sendMailBtn">Submit
      </button>
    </div>

  </div>

  <div class="clear"></div>

</form>
  1. my jquery ajax code
jQuery(document).ready(function() {
  jQuery("#forma").submit(function(e) {

    e.preventDefault();
    e.stopImmediatePropagation();

    jQuery('#sendmail').html('Please wait...');

    jQuery.ajax({
      type: "post",
      url: './contact-form.php',
      data: jQuery("#forma").serialize(),
      dataType: "json",
      success: function(data) {

        jQuery('p.error-red').html('');


        if ('success' == data.result) {


          //jQuery("#forma")[0].reset();

          //jQuery('#response').append('<p class="successMessage">' + data.msg + '</p>');
          //  jQuery('#sendmail').html('Submit');
          window.location.href = "https://www.mrpmediaworks.com/thanks.php";

        }

        if ('error' == data.result) {

          var arr = data.msg;
          var element_arr = data.fieldname;
          count = '0';
          jQuery.each(arr, function(index, value) {
            if (value.length != 0) {

              jQuery('input[name=' + element_arr[count] + ']').parent('.group').find('p.error-red').html(value);
            }
            count++;
          });
          jQuery('#sendmail').html('Submit');

        }

      }
    });

    return false;
  });
});
  1. my php code

<?php
      session_start();
      require_once('config.php');

      $email = '';
      $fieldname = array();
      $msg = array();
      $result = '';
      $count = '';
      $name =  $_Post["name"];
      $contact =  $_Post["mobileNo"];
      $email =  $_Post["eMail"];
      $msg_query =  $_Post["message"];
      $captcha =  $_Post["captcha"];

      echo $name;
      die();
?>

just testing code

same code works with get

I have tried removing action from form and may possible combination but noting worked. after edit

      4. .htaccess file
                # browser requests PHP
            RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
            RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]

           # check to see if the request is for a PHP file:
           RewriteCond %{REQUEST_FILENAME}\.php -f
           RewriteRule ^/?(.*)$ /$1.php [L]

this is the only content in my htaccess file

Logan9798
  • 25
  • 4
  • Where is your form, I can't see in your code. – Mohit Tanwani Sep 29 '16 at 09:08
  • Duplicate of http://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php – Jackson Sep 29 '16 at 09:09
  • @AnkitShah not really a duplicate. He needs to return json that has data.result attribute – mplungjan Sep 29 '16 at 09:13
  • I havnt posted my complete php code but it has the echo json_encode($result); but it is not working. i m trying to say I m not able to pass data from form to the php page which is here named as contact-form.php . – Logan9798 Sep 29 '16 at 09:15

2 Answers2

1

I think you have messed up with your htaccess file . if possible paste your .htaccess file code here.

Editing My answer

Bingo found the problem you are removing .php extension from you files but in ajax request you have posted data to contact-form.php .Try removing .php extension from your ajax request . Try it hope this will help.

thedudecodes
  • 1,479
  • 1
  • 16
  • 37
  • I have added my htaccess code please have a look and please tell me whats wrong in it. Hope you can help – Logan9798 Sep 29 '16 at 10:34
0

Just make one function to handle the ajax request. Then process the request inside the function.To do that,

Remove the action from form. and send the request to the function on on submit see the below eg.

 <form class="forma" id="forma" method="POST"  action="javascript:void(0);" name="myForm" autocomplete="off"  onsubmit="sendContactRequest();">

Then the ~sendContactRequest()~ will handle your ajax request.

function sendContactRequest(){
        jQuery('#sendmail').html('Please wait...');
        $.post("contact-form.php",jQuery("#forma").serialize(),function(result,status,xhr) { 
            if( status.toLowerCase()=="error".toLowerCase() ) { 
                alert("An Error Occurred.."); // or do something that you can handle the error request.
            }else {  //success response
            }
        });
    }

Please try this.In my case it is working perfectly.

Pranav MS
  • 2,235
  • 2
  • 23
  • 50