JavaScript Ajax/PHP issue
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Ajax/PHP issue

  1. #1
    Join Date
    Aug 2002
    Location
    Ocean Springs, MS
    Posts
    60

    Ajax/PHP issue

    Can someone please help me with this code by looking over it for something obvious? The problem seems to be that the embedded JavaScript code is not being executed and, therefore, the PHP code is also skipped. Both, the HTML and the PHP files are in the root directory.

    html/js:

    Code:
    <div class="container"> 
      <!-- Contacts -->
      <div id="contacts">
        <div class="row"> 
          <!-- Alignment -->
          <div class="col-sm-offset-3 col-sm-6">
            <p>&nbsp;</p>
            <p>Some header message here</p>
            <p>&nbsp;</p>
            <form name="contact" class="well" id="contact">
              <legend>Contact Form</legend>
              <div class="control-group">
                <div class="controls">
                  <input type="text" class="form-control" placeholder="Name" id="name" />
                  <p class="help-block"></p>
                </div>
              </div>
              <div class="control-group">
                <div class="controls">
                  <input type="email" class="form-control" placeholder="Email" id="email" required/>
                </div>
              </div>
              <div class="control-group">
                <div class="controls">
                  <textarea rows="10" cols="100" class="form-control" placeholder="Message" id="message" style="resize:none"></textarea>
                </div>
              </div>
              <div id="success"> </div>
              <button type="submit" class="btn btn-primary pull-right" id="submit">Send</button>
              <button type="reset" class="btn btn-default pull-right" id="res">Reset</button>
              <br />
            </form>
          </div>
        </div>
      </div>
    </div>
    <script>
     $(function() {
        $("button#submit").click(function(event){
            event.preventDefault();
            $.ajax({
                type: "POST",
                url: "process.php",
                data: $('form.contact').serialize(),
                success: function(){
                        alert("success");
                },
                error: function(){
                    alert("failure");
                }
            });
        });
    });
    </script>
    </body>
    </html>
    php:

    PHP Code:
    <?php
        
    if (isset($_POST['name'])) {
            
    $name strip_tags($_POST['name']);
            
    $email strip_tags($_POST['Email']);
            
    $messagestrip_tags($_POST['message']);
            echo 
    "Name      =".$name."</br>";   
            echo 
    "Email     =".$email."</br>";  
            echo 
    "Message       =".$message."</br>";    
            echo 
    "<span class=\"label label-info\" >your message has been submitted .. Thank you</span>";
        }
    ?>
    Last edited by PeejAvery; August 28th, 2014 at 07:21 PM. Reason: Added proper BB tags for code.

  2. #2
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,870

    Re: Ajax/PHP issue

    Not sure what web browser you are using, but almost all have consoles to help with JavaScript debugging. I would highly suggest trying Firebug for Firefox to see what is failing.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center