Advanced Search

Results 1 to 4 of 4

Thread: PHP Form response

  1. #1
    Join Date
    Aug 2011
    Location
    both, Germany and Bulgaria
    Posts
    54
    Thanks
    14
    Thanked 10 Times in 10 Posts

    Question PHP Form response

    Hello guys,

    trying to organize a simple PHP-send-form-input-over-eMail-form, I've found one PHP example on the w3schools site how to do it. The example is for one whole page to reload and react when something was written into the input fields and submit is pushed.

    My question is: how can I change the PHP part to replace the content of one specific DIV only, without reloading the whole page. The same DIV, wherein that PHP form is placed. With other words, I'd like on submit to remove the form and on its place to show only "Thank you", or "Invalid input" with back button to show the form again. All the rest page content, shall remain in place and not be reloaded.

    Thanks for your help.

    Cheers
    Last edited by XManBG; 08-25-2011 at 09:44 PM. Reason: Resolved

  2. #2
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    This is accomplished with AJAX. If you're going to use AJAX, I recommend doing it the jQuery way. Here's a quick form I wrote with the code from the w3schools site.

    PHP Code:
    <html>
    <body>
    <?php
    function spamcheck($field)
      {
      
    //filter_var() sanitizes the e-mail
      //address using FILTER_SANITIZE_EMAIL
      
    $field=filter_var($fieldFILTER_SANITIZE_EMAIL);

      
    //filter_var() validates the e-mail
      //address using FILTER_VALIDATE_EMAIL
      
    if(filter_var($fieldFILTER_VALIDATE_EMAIL))
        {
        return 
    TRUE;
        }
      else
        {
        return 
    FALSE;
        }
      }

    if (isset(
    $_REQUEST['email']))
      {
    //if "email" is filled out, proceed

      //check if the email address is invalid
      
    $mailcheck spamcheck($_REQUEST['email']);
      if (
    $mailcheck==FALSE)
        {
        echo 
    "Invalid input";
        }
      else
        {
    //send email
        
    $email $_REQUEST['email'] ;
        
    $subject $_REQUEST['subject'] ;
        
    $message $_REQUEST['message'] ;
        
    mail("someone@example.com""Subject: $subject",
        
    $message"From: $email);
        echo 
    "Thank you for using our mail form";
        }
      }


    ?>
    <script type="text/javascript">
    function sendForm() {
    var dataSend = "?subject=" + $("#subject").val() + "&message=" + $("#message").val() + "&email=" + $("#email").val();

    $.post("<?php echo $_SERVER[PHP_SELF]; ?>", dataSend, function(data) {
    $("#responseDiv").html(data);
    });

    </script>

    <div id="responseDiv"></div>
    <form method='post' action='<?php echo $_SERVER[PHP_SELF]; ?>' onsubmit="sendForm()" id="myForm">
      Email: <input name='email' id="email" type='text' /><br />
      Subject: <input name='subject' id="subject" type='text' /><br />
      Message:<br />
      <textarea name='message' id="message" rows='15' cols='40'>
      </textarea><br />
      <input type='submit' id="submit" name="submit" />
      </form>

    </body>
    </html>
    - Josh

  3. The Following User Says Thank You to JShor For This Useful Post:

    XManBG (08-27-2011)

  4. #3
    Join Date
    Aug 2011
    Location
    both, Germany and Bulgaria
    Posts
    54
    Thanks
    14
    Thanked 10 Times in 10 Posts

    Arrow

    Thanks Josh, for your reply. The code you've suggested assumes again, that the <form>-page and the result-page, are the same. That's not what I'd like to use. But I'm also not sure, if my request is possible. I'll try to systematize it:

    1) One HTML page named contacts.html is loaded, where one div containing form is ready to be filled out.
    HTML Code:
    <div id="result"><form id="myForm">...something...</form></div>
    2) The user enters data into the fields and pushes button Submit.

    3) The page contacts.html remains in place.

    4) Using PHP (and without loading new page or reloading the same page) the input data shall be checked and sent to an email. Maybe you mean here, that I have to use AJAX instead, but if so, I have not understand how... What I mean here is, that I'd like to call a function on the form action, instead to load a new page (or reload the PHP page).

    5) The PHP (or AJAX) shall send one sentence in return "Thank you!" or "Invalid input!", written into the same <div id="result">, where the <form> was before.

    That's all.

  5. #4
    Join Date
    Aug 2011
    Location
    both, Germany and Bulgaria
    Posts
    54
    Thanks
    14
    Thanked 10 Times in 10 Posts

    Cool Resolution

    Thanks again. I've found a resolution after studying the $.post of AJAX. Here is how I've done it, assuming the <form> is placed inside a <div id="result">:

    Code:
    function sendForm() {
    
    	//.... some if-s for checking field content and return with "false" if not correct ...
    
    	$.post("mailform.php", { email : "someones@email.com" , subject : "Important subject" , message : "Some words about your concern..." })
    	.success(function(){
    		//...what to do if success;
    		$("#result").empty().append("Thank you!");
    		//...
    	 })
    	.error(function(){
    		//...and what to do if failure;
    		$("#result").empty().append("Our apologies, there was an unexpected error. Our server is using Windows. :D");
    		//...
    	});
    
    };
    PHP Code:
    mailform.php has only sending purpose:

    <?php

    $email 
    $_REQUEST['email'] ;
    $subject $_REQUEST['subject'] ;
    $message $_REQUEST['message'] ;

    mail("ouremail@email.com"$subject$message"From:" $email);

    ?>

Bookmarks

Posting Permissions

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