简体   繁体   English

Ajax表单提交和响应中存储的数据?

[英]Ajax form submission and storing the data from response?

I have form which is used for sending sms now am sending the data on submit to sms.php page and executing the process. 我有用于发送短信的表格,现在将提交的数据发送到sms.php页面并执行该过程。 Now I need to submit the data in form without moving to sms.php page. 现在,我需要以表格形式提交数据,而无需移至sms.php页面。 It should be executed in the form page itself by using ajax method I have done that but also it redirects to sms.php. 它应该使用我已经执行过的ajax方法在表单页面本身中执行,但是它也重定向到sms.php。 I need it to be done and the same page and the json response obtained should be stored in the database. 我需要完成它,并且将获得的同一页和json响应存储在数据库中。

How can I do this? 我怎样才能做到这一点? Help me Thanks in advance. 帮帮我,谢谢。

Form used for sending sms: 用于发送短信的表格:

   <form role="form" method="POST" action="sms.php">
        <div class="form-group row">
            <label class="col-sm-4 form-control-label">Select Date Range<span class="text-danger">*</span>
            </label>
            <div class="col-sm-7">
                <div class="input-daterange input-group" id="date-range">
                    <input type="text" class="form-control" name="start" />
                    <span class="input-group-addon bg-custom b-0">to</span>
                    <input type="text" class="form-control" name="end" />
                </div>
            </div>
        </div>
        <div class="form-group row">
            <label class="col-sm-4 form-control-label">Select Brand<span class="text-danger">*</span>
            </label>
            <div class="col-sm-7">
                <select class="form-control" name="brand" id="brand" required>
                    <option value="">Select Brand</option>
                    <option value="3">test1</option>
                    <option value="2">test2</option>
                    <option value="1">test3</option>
                </select>
            </div>

        </div>

        <div class="form-group row">
            <div class="col-sm-8 col-sm-offset-4">
                <input type="submit" class="btn btn-primary  btn-sm" value="Submit">
            </div>
        </div>
    </form>

Ajax used to send data 用于发送数据的Ajax

$('sms').on('submit', function (e) {

          e.preventDefault();

          $.ajax({
            type: 'post',
            url: 'sms.php',
            data: $('form').serialize(),
            success: function () {
              alert('form was submitted');
            }
          });

        });

sms.php sms.php

<?php

    if(isset($_POST['submit'])){
       $senderid =$_POST['senderid'];
       // echo $senderid;
       $message = rawurlencode($_POST['message']);
       $mobile = isset($_POST['mobilenumber']) ? $_POST['mobilenumber'] : '';
       sendSMS($mobile,$message,$senderid);
    }

function sendSMS($mobile,$message,$senderid){

$user = "asdhaskbdjasdjbfjk";
$url = 'http://login.smsgatewayhub.com/api/mt/SendSMS?APIKey='.$user.'&senderid='.$senderid.'&channel=2&DCS=0&flashsms=0&number='.$mobile.'&text='.$message.'&route=16;';

$ch=curl_init($url);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,"");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,2);
$data = curl_exec($ch);
print($data); /* result of API call*/

}

?>

Response obtained: 获得的回应:

{"ErrorCode":"000","ErrorMessage":"Success","JobId":"112591825","MessageData":[{"Number":"0123456789","MessageId":"mU9WfV6hLEyNAQQI63BzNw"}]}

Your selector in jquery code $('sms') is wrong. jQuery代码$('sms')选择器错误。 And to stop changing the page, you should use click event and stop actually submitting the form like this: 要停止更改页面,您应该使用click事件并停止实际提交如下表单:

$('.btn-sm').on('click', function (e) {
    e.preventDefault();
    $.ajax({
        type: 'post',
        url: 'sms.php',
        data: $('form').serialize(),
        success: function () {
          alert('form was submitted');
        }
    });
});

You can see from the above given code snippet that, I have used click event of the submit button, and also written e.preventDefault() in first line, so it will stop the form to submit but it will just initiate ajax call. 从上面给出的代码片段中可以看到,我已经使用了提交按钮的click事件,并且还在第一行中编写了e.preventDefault() ,因此它将停止提交表单,但只会启动ajax调用。 So SMS will be sent and you will be on the same page. 因此,将发送SMS,您将在同一页面上。 No redirection will be there. 没有重定向。

In your code I found that you added action in your page: 在您的代码中,我发现您在页面中添加了操作:

 <form role="form" method="POST" action="sms.php">

remove that action and post from form as because you included inside your script tag 删除 的行动和,因为你需要包括你的脚本标签中从形式 发布

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM