duanhuo7441 2013-07-13 16:52
浏览 31
已采纳

PHP Form to Email,如何选择用户输入的输入,PHP语法

I am making a simple mobile application that sends an email to the user with their GPS location and other form input. I am not worried about the validation now I just want to get the form sent to email, although help with that would be appreciated.

Process.php and especially the confirmation is the main problem. I do not know how to correctly put the GPS coordinates into the confirmation email nor display the form/email sent verification to the user on successful submission. I do not know how to select the text input of the GPS Lat and Longitude. Syntax is also obviously incorrect.

<head>
            <meta content="text/html; charset=windows-1252" http-equiv="content-type">
            <title>My Page</title>
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css">
            <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
            <script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>
          </head>
          <body>
            <div data-role="page" id="page">
              <div data-role="header">
                <h1>Hitch Fox -Travel with smarts</h1>
              </div>
              <div data-role="content"> * = required
                <form id="myform" action="process.php" method="POST">


                  <div data-role="fieldcontain"> <label for="email"><em> * </em>
                      Email:</label> <input name="email" id="verify email" value="" required="required"

                      type="email"> </div>
                  <div data-role="fieldcontain"> <label for="verify email"><em> * </em>
                      Verify Email: </label> <input name="verify email" id="verify email"

                      value="" required="required" type="email"> </div>
                  <br>
                  <div data-role="fieldcontain"> <label for="GPS Latitude"><em> * </em>
                      GPS Lat </label> <input name="GPS Lat" id="GPSlat" value="" type="text">
                  </div>
                  <br>
                  <div data-role="fieldcontain"> <label for="GPS Longitude"><em> * </em>
                      GPS Long </label> <input name="GPS Long" id="GPSlong" value="" type="text">
                  </div>
                  <br>
                  <button  id="SetGPS"> Set GPS</button>
                  <div data-role="fieldcontain"><label for="License Plate"><em> * </em>License
                      Plate: </label> <input name="License Plate" id="LicensePlate" value=""

                      required="true" type="text"> </div>
                </form>
                <button id="submit">Submit</button>
                <div dat-role="fieldcontain"id="ack"></div> //confirmation of form sent to email goes here 

                <!--/fieldcontain-->
                <div data-role="footer">
                  <h4>footer</h4>
                </div>
                <!--/content-->

              </div>
              <!--/page-->        



     <script type="text/javascript">


                            var lat, lng;

        navigator.geolocation.getCurrentPosition(function(position)
        { 
            lat = position.coords.latitude;
            lng = position.coords.longitude;
            console.log( lat + ":" + lng);
        });

        // ...

        $("#SetGPS").click(function()
        {
            $("input#GPSlat").val(lat);
            $("input#GPSlong").val(lng);
        });
                  </script>

              <script type="text/javascript">

            $("submit").click(function() {

                .post( $("myForm").attr("action"),
                       $("myForm" :input").serializeArray(),
                           function(info) {

                           $("#ack").empty();
                           $("#ack").html(info);
                                clear();
                        });

                $("#myForm").submit( function ()   {
                    return false;
                });    
            });

            function clear()  {

                $("myForm :input").each ( function() {
                        $(this).val("");
                    });    
              )          
              </script> 

        <?php     

        $Email          = $ _POST['Email'];
        $GPS Lat        = $ _POST['GPS Lat'];
        $GPS Long       = $ _POST['GPS Long']; 
        $License Plate  = $ _POST['License Plate'];

        $to      = "$Email"
        $from    = "$Email"
        $subject = "Your last GPS Location"
        $body    = "Your last GPS location was - latitude:" .$GPS Lat " "  "longitude:" .$GPS Long "and you were picked up by vehicle license:" .$License Plate // problems here 
        mail ($to, $from, $subject, $body)
        echo  $ack "message sent"   // php syntax is wrong no doubt

        ?>
  • 写回答

2条回答 默认 最新

  • dongyinting3179 2013-07-13 16:58
    关注

    You dont need to put input before, you have already IDs for the Lat + Lng:

    $("#SetGPS").click(function()
            {
                $("#GPSlat").val(lat);
                $("#GPSlong").val(lng);
            });
    

    Dont define vars with a space in between:

    $GPSLat        = $ _POST['GPSLat'];
    $GPSLong       = $ _POST['GPSLong']; 
    $LicensePlate  = $ _POST['LicensePlate'];
    

    To access the inputs use this:

    $("#GPSlat").val();
    $("#GPSlat").val();
    

    EDIT:

    I did some edits on your code. now you can add validation as already mentioned + mail functionality + structuring the code.

    index.html

    <html>
    <head>
                <meta content="text/html; charset=windows-1252" http-equiv="content-type">
                <title>My Page</title>
                <meta name="viewport" content="width=device-width, initial-scale=1">
                <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css">
                <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
                <script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>
    </head>
    
    <body>
                <div data-role="page" id="page">
                  <div data-role="header">
                    <h1>Hitch Fox -Travel with smarts</h1>
                  </div>
                  <div data-role="content"> * = required
                    <form id="myform">
    
    
                      <div data-role="fieldcontain"> <label for="email"><em> * </em>
                          Email:</label> <input name="mail" id="verify email" value="" required="required"
    
                          type="email"> </div>
                      <div data-role="fieldcontain"> <label for="verify email"><em> * </em>
                          Verify Email: </label> <input name="vmail" id="verify email"
    
                          value="" required="required" type="email"> </div>
                      <br>
                      <div data-role="fieldcontain"> <label for="GPS Latitude"><em> * </em>
                          GPS Lat </label> <input name="lat" id="GPSlat" value="" type="text">
                      </div>
                      <br>
                      <div data-role="fieldcontain"> <label for="GPS Longitude"><em> * </em>
                          GPS Long </label> <input name="lng" id="GPSlong" value="" type="text">
                      </div>
                      <br>
                      <button  id="SetGPS"> Set GPS</button>
                      <div data-role="fieldcontain"><label for="License Plate"><em> * </em>License
                          Plate: </label> <input name="lic" id="LicensePlate" value=""
    
                          required="true" type="text"> </div>
                    </form>
                    <button id="SendForm">Submit</button>
                    <div dat-role="fieldcontain" id="ack"></div> //confirmation of form sent to email goes here 
    
                    <!--/fieldcontain-->
                    <div data-role="footer">
                      <h4>footer</h4>
                    </div>
                    <!--/content-->
    
                  </div>
                  <!--/page-->        
    
    
    
         <script type="text/javascript">
    
    //VAR    
                                var lat, lng;
    
    // GET USER COORDS                          
            navigator.geolocation.getCurrentPosition(function(position)
            { 
                lat = position.coords.latitude;
                lng = position.coords.longitude;
                console.log( lat + ":" + lng);
                $("#GPSlat").val(lat);
                $("#GPSlong").val(lng);
            });
    
            // ...
    
            $("#SetGPS").click(function()
            {
                $("#GPSlat").val(lat);
                $("#GPSlong").val(lng);
            });
    
    
    //SUBMIT       
                $("#SendForm").click(function() {
    
                $.post('process.php', $("#myform").serialize(), function(data) {
                $('#ack').html(data);
                }); 
                });
    
    </script>
         </body></html>
    

    process.php

       <?php     
    
            $email          = $_POST['mail'];
            $lat            = $_POST['lat'];
            $long           = $_POST['lng']; 
            $licence        = $_POST['lic'];
    
            echo 'PHP recieved following POST values <br/>';    
            echo $email."<br/>";
            echo $lat."<br/>"; 
            echo $long."<br/>";
            echo $licence."<br/>";
    
    /*  
            $to      = "$Email"
            $from    = "$Email"
            $subject = "Your last GPS Location"
            $body    = "Your last GPS location was - latitude:" .$GPS Lat " "  "longitude:" .$GPS Long "and you were picked up by vehicle license:" .$License Plate // problems here 
            mail ($to, $from, $subject, $body)
            echo  $ack "message sent"   // php syntax is wrong no doubt
    */
      ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值