简体   繁体   English

Ajax:功能不起作用

[英]Ajax : function is not working

I don't know why my code does not work onclick, but there is no pop up. 我不知道为什么我的代码无法工作,但没有弹出。 The AJAX function var phone and id got data, but the AJAX did not. AJAX函数var phoneid数据,但AJAX没有。 Is there anything wrong with the script? 脚本有什么问题吗? Does the AJAX need a jQuery library? AJAX需要一个jQuery库吗?

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/core.js"></script>
<script type="text/javascript">
  function clickButton() {
    var phone = document.getElementById('phone').value;
    var id = document.getElementById('id').value;
    $.ajax({
      type: "post",
      url: "userDetaiAjax.php",
      data: {
        'phone': phone,
        'id': id
      },
      cache: false,
      success: function(html) {
        alert('Data Send');
        $('#msg').html(html);
      }
    });
    return false;
  }
</script>
<div class="col">
  <div class="form-group">
      <h6>
        <label>Name :</label>
        <?php echo $name ?>
      </h6>
      <h6>
        <label>Email :</label>
        <?php echo $email ?>
      </h6>
      <input type="" name="phone" id="phone">
      <input type="" name="id" id="id" value="<?php echo $_SESSION[" staff_id "];?>" hidden>
      <h6>
        <label>Phone :</label>
        <?php echo $phone ?>
      </h6>
      <input type="submit" name="aa" value="submit2" onclick="return clickButton();">
    </form>
    <p id="msg"></p>
  </div>
</div>
</div>
</div>

for the url 为网址

<?php 
$phone = $_POST['phone'];
$id = $_POST['id'];
echo 'success';
echo $phone;
echo $id;

$a='"USR_ID"';
$b='"phone"';
include 'conn.php';
if (!$conn) {
    $e = oci_error();
    trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);

}

   $query = "update usr_web SET phone=:c1 WHERE usr_id=:c2";

              $stid = oci_parse($conn, $query);

   oci_bind_by_name($stid, ":c1", $phone);
   oci_bind_by_name($stid, ":c2", $id);


                                 $result = oci_execute($stid);
                                 oci_free_statement($stid);
                                 oci_commit($conn);
                                  oci_close($conn);
                            echo $result;

?>

When you open the console of your browser you should see an error message like "ReferenceError: $ is not defined". 当您打开浏览器的控制台时,您应该看到一条错误消息,例如“ReferenceError:$ is not defined”。 So it seems that $ is not part of core.js which you have loaded. 所以似乎$不是你加载的core.js的一部分。 Just load the full version ( https://code.jquery.com/jquery-3.3.1.min.js ) and it should work. 只需加载完整版( https://code.jquery.com/jquery-3.3.1.min.js ),它应该工作。

U have error in your console: 您的控制台中有错误:

$ is not defined $未定义

Which mean u did not include jquery, so u should add this code after ajax include: 这意味着你没有包含jquery,所以你应该在ajax之后添加这段代码:

<script
  src="https://code.jquery.com/jquery-3.3.1.min.js"
  integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
  crossorigin="anonymous"></script>

U can chack your console errors with F12 key in browser and tab Console . 您可以使用浏览器和tab Console F12 key来查看控制台错误。

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

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