douzhi19900102 2016-02-02 16:09
浏览 30

在发送到远程API之前捕获PHP表单字段

I have a form which sends its form fields to a remote locations through a javascript file which involves remote API.

I want to capture a single form field through PHP before it is sent to remote API.

My form inside a PHP file and the code to capture email field

<form id="mycustomform" method="post" action="" role="form">
                <div class="row">
                      <div class="form-group col-lg-4">
                        <label for="Email-input">Email</label>
                        <input type="email" class="form-control" id="Email_Address-input" placeholder="Your Email" name="vemail" parsley-type="email" required>
                    </div>
                    <div id="submit" class="form-group col-lg-12">
                        <input type="hidden" name="submit" value="contact">
                        <button type="submit" class="btn btn-success">Submit</button>
                    </div>
                </div>
            </form>
<?php 
if(isset($_POST['submit'])){
    $to = "myself@email.com";
    $subject = "New Entry";
    $message = $_POST['vemail'];
    mail($to,$subject,$message);
    }
?>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
    <script src="http://www.mywebsite.com/javascript.js"></script>

javascript.js

$("#mycustomform").submit(function(event) {
var lead = {};  
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10) {
dd='0'+dd
} 
if(mm<10) {
    mm='0'+mm
} 
today = mm+'/'+dd+'/'+yyyy; 
lead['Email'] = $('#Email_Address-input').val();
$.post("http://remoteserver.com/Token", {grant_type: 'password', username: 'myusername', password: 'mypassword'})
    .done(function(data) {
        $.ajax({
            url: 'http://remoteserver.com/api/',
            type: 'POST',
            dataType: 'json',
            contentType: 'application/json; charset=utf-8',
            data: JSON.stringify(lead),
            beforeSend: function (xhr) {
                xhr.setRequestHeader('Authorization', 'Bearer ' + data.access_token)
            }
                }).done(function(data) {
                    window.location = "http://www.mywebsite.com/thank-you.html";        
                }).fail(function(jqXHR, textStatus, errorThrown) {
            });
        }).fail(function(jqXHR, textStatus, errorThrown) {
    });
event.preventDefault(); 
});

The problem is that once the javascript is involved, the field is not captured and an email is not sent. As soon as I remove the javascript reference, it captures the form field and sends an email without problem.

Can someone please help how would I achieve this?

Basically I want to send the email field to myself via mail() function and then have the PHP file to process the field forms and send them to remote API.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
    • ¥15 Vue3地图和异步函数使用
    • ¥15 C++ yoloV5改写遇到的问题
    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
    • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
    • ¥15 帮我写一个c++工程
    • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
    • ¥15 关于smbclient 库的使用