简体   繁体   English

如何从Java脚本发送和$ _GET到PHP?

[英]How to Send and $_GET from Java Script to PHP?

I have made a Script sendsms.php which executes and sends SMS with HTTP API. 我制作了一个脚本sends.php,该脚本执行并使用HTTP API发送SMS。 I call sendsms.php with a Java script (Button) and get back results in a text input window. 我使用Java脚本(按钮)调用sendms.php,并在文本输入窗口中获取结果。

When I press my Java Button the phone number is automatically sent to sendsms.php and it executes. 当我按下Java按钮时,电话号码会自动发送到sendsms.php并执行。 Below in my CODE you can see how the #phone is sent (with Java) and how its retrieved by sendsms.php 在我的代码下面,您可以看到#phone的发送方式(使用Java)以及sendms.php如何获取它。

My question is: Now I want add and SEND #nick_name together with #phone . 我的问题是:现在,我想与#phone一起添加和发送#nick_name How should I do that? 我应该怎么做?

My Jave Button: 我的Jave按钮:

<script type="text/javascript">
    $(document).ready(function() {
        $('.smsbutton').click(function() {
            var val = $('#phone').val();
            $.get('http://mydomain.com/sendsms.php', {phone: val}, function(data) {            
                result = $.parseJSON(data);
                $("input[name='avaresultsms']").val(result.avaresultsms);
            });
        });
    });
    </script>
<br />
<input type="text" name="avaresultsms" value="" style="width: 370px;" readonly="readonly" />
<input id="smsbutton" name="smsbutton" type="button" class="smsbutton" value="SEND SMS">

And here is sendsms.php: 这是sendsms.php:

<?php

$phone = $_GET['phone'];
$smstophone = str_replace("+", "", $phone);

$sendapi = 'http://sms.com/api.php=sendsms&user=MYUSERNAME&password=MYPASS&&from=Escort%20Home&to='.$smstophone.'&text=Hello%20'.$nick_name.'%20Test1%20test2';
$smsrsult = file_get_contents($sendapi);
$result['avaresultsms'] = $smsrsult;
echo json_encode($result);
?>

As you can see I use var val = $('#phone').val(); 如您所见,我使用var val = $('#phone').val(); in Java Button so with sendsms.php I can get it with: $phone = $_GET['phone']; 在Java Button中,使用sendsms.php可以通过以下方式获得它: $phone = $_GET['phone'];

But now I also want to get $nick_name = $_GET['nick_name']; 但是现在我也想得到$nick_name = $_GET['nick_name']; What should I add to Java script? 我应该在Java脚本中添加什么?

Thank you very much for your help. 非常感谢您的帮助。

THIS WORKED FOR ME: 这对我有用:

var nickname = $('#nick_name').val();
$.get('http://mydomain.com/sendsms.php', {phone: val, nick_name: nickname}, function(data) {

try something like this 尝试这样的事情

             var val = $('#phone').val();
             var nick_name_val = 'sample name';
             $.get('http://mydomain.com/sendsms.php', {phone: val,nick_name: nick_name_val}, function(data) {            
                 result = $.parseJSON(data);
                 $("input[name='avaresultsms']").val(result.avaresultsms);
             });

Just add the nick name like how you got phone and add it to the object. 只需添加昵称即可,就像您如何获得电话一样,并将其添加到对象中。

var nickname = $('#nickname');
$.get('http://mydomain.com/sendsms.php', {phone: val, nick_name: nickname}, function(data)

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

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