简体   繁体   中英

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. I call sendsms.php with a Java script (Button) and get back results in a text input window.

When I press my Java Button the phone number is automatically sent to sendsms.php and it executes. Below in my CODE you can see how the #phone is sent (with Java) and how its retrieved by sendsms.php

My question is: Now I want add and SEND #nick_name together with #phone . How should I do that?

My Jave Button:

<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:

<?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(); in Java Button so with sendsms.php I can get it with: $phone = $_GET['phone'];

But now I also want to get $nick_name = $_GET['nick_name']; What should I add to Java script?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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