duanjiati1755 2019-07-03 10:43
浏览 99
已采纳

Ajax html输入值显示为空

I am trying to submit a form via ajax post to php but the value of the input tag appears to empty.

I have cross-checked defined class and id and it seems ok. I don't where my mistake is coming from. Here is the code

index.html

<div class="modal">
  <div class="first">
  <p>Get notified when we go <br><span class="live">LIVE!</span></p>
  <input type="text" class="input" id="phone" placeholder="Enter your email adress" />
  <div class="arrow">
    <div class="error" style="color:red"></div>
    <div class="validator"></div>
  </div>
  <div class="send">
    <span>Subscribe</span>
  </div>
  </div>
  <div class="second">
    <span>Thank you for<br />subscribing!</span>
  </div>

</div>
  <script src='jquery-3.3.1.min.js'></script>



    <script  src="script.js"></script>

script.js

$(document).ready(function(){
  function validatePhone(phone) { 
    var re = /^((\+[1-9]{1,4}[ \-]*)|(\([0-9]{2,3}\)[ \-]*)|([0-9]{2,4})[ \-]*)*?[0-9]{3,4}?[ \-]*[0-9]{3,4}?$/;
    return re.test(phone);
  } 

  $('.input').on('keyup',function(){
    var formInput = $('.input').val();
    if(validatePhone(formInput)){

      $('.validator').removeClass('hide');
      $('.validator').addClass('valid');

      $('.send').addClass('valid');
    }
    else{
      $('.validator').removeClass('valid');
      $('.validator').addClass('hide');

      $('.send').removeClass('valid');
    }
  });

  var phone = $('#phone').val();

  var data =
  'phone='+phone;
  $('.send').click(function(){
    $.ajax({
      type:"POST",
      url:"subscribe.php",
      data: data,
      success: function(data){
        alert(data);
        if (data ==1) {
          $('.modal').addClass('sent');
        }else{
          $('.error').html("Error String:" +data);
        }
      }
    })

  });
});

subscribe.php

```php
$phone = htmlentities($_POST['phone']);

if (!empty($phone)) {
    echo  1;
}else{
    echo "Phone number cannot be empty";
}
```

An empty results with the error code is all I get. Can any one help me out here with the mistakes I am making. Thanks

  • 写回答

2条回答 默认 最新

  • dongyongkui6329 2019-07-03 11:01
    关注

    Change next

    JS:

    $('.send').click(function(){
        $.ajax({
          type:"POST",
          url:"subscribe.php",
          data: data,
          success: function(data){
            alert(data);
            if (data ==1) {
              $('.modal').addClass('sent');
            }else{
              $('.error').html("Error String:" +data);
            }
          }
        })
    
      });
    

    to

      $('.send').click(function(){ 
      var data = $('#phone').val();
        $.ajax({
          type:"POST",
          url:"subscribe.php",
          data: {phone: data},
          success: function(data){
            alert(data);
            if (data ==1) {
              $('.modal').addClass('sent');
            }else{
              $('.error').html("Error String:" +data);
            }
          }
        }); 
      });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算