简体   繁体   English

HTML输入变量未使用AJAX传递到PHP文件

[英]HTML input variable not passing to PHP file using AJAX

The i'm working on needs the user to enter their name and then they will click a submit button and their details will appear below based on what the PHP collects on them. 我正在处理的工作需要用户输入名称,然后他们将单击“提交”按钮,并且根据PHP收集的内容,其详细信息将显示在下面。 Thing is all this needs to be done on the same page, i've used Ajax to do this however the input of the text box isn't being passed to the PHP page at all. 所有这些都是需要在同一页面上完成的,我已经使用Ajax做到了,但是文本框的输入根本没有传递到PHP页面。 Could someone advise on where i'm going wrong as i've really ran out of answers on this one 有人可以告诉我我要去哪里了吗,因为我真的没有关于这个的答案

HTML side: HTML面:

<form action="summonerSearch.php" method="post">
        <input type="text" name="summNameEntered">   
</form>




<button type="button">Click Me</button>
<p></p>
<script type="text/javascript">
    var sname = $("#summNameEntered").val();
    $(document).ready(function(){
        $("button").click(function(){
            $.ajax({
                type: 'POST',
                url: 'summonerSearch.php',
                data: { // this is new
                summNameEntered: $("input[name='summNameEntered']").value
                },
                success: function(data) {
                    $("p").html(data);
                }

            });
   });
});
</script>

PHP side: PHP方面:

   echo('their name is'.$_POST["summNameEntered"]);

You need to give the input field the ID summNameEntered as that is what you're requesing in the AJAX call: 您需要为输入字段提供ID summNameEntered因为这是您在AJAX调用中要查询的内容:

<form action="summonerSearch.php" method="post">
        <input type="text" id="summNameEntered" name="summNameEntered">   
</form>

Or you can simply change var sname = $("#summNameEntered").val(); 或者,您可以简单地更改var sname = $("#summNameEntered").val(); to var sname = $(".summNameEntered").val(); var sname = $(".summNameEntered").val();

And summNameEntered: $("input[name='summNameEntered']").value should be summNameEntered: $("input[name='summNameEntered']").val() 并且summNameEntered: $("input[name='summNameEntered']").value应该是summNameEntered: $("input[name='summNameEntered']").val()

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

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