I'm trying to use ajax on a select tag with 2 options, but it's not getting the $_POST for some reason. It prints out the "---", but it does not print out the $_POST value, which is either 1 or 2. I'm not sure what I did wrong. Please take a look at my code:

我试图在带有2个选项的select标签上使用ajax,但由于某种原因它没有获得$ _POST。它打印出“---”,但它不打印$ _POST值,即1或2.我不确定我做错了什么。请看一下我的代码:

newtest.php

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<script type = "text/javascript"> 

function ajax(url,type,theName,id) {

      $.ajax({
           type: "POST",
           url: url,
           data: { select: $(type+'[name='+theName+']').val()},
           error: function(xhr,status,error){alert(error);},
           success:function(data) {
             document.getElementById( id ).innerHTML = data;
           }

      });

}

</script>

<?php

echo "<select name = 'name' onchange = 'ajax(\"newtestx.php\",\"input\",\"name\",\"output\")'>";
echo "<option value = '1'> 1 </option>";
echo "<option value = '2'> 2 </option>";
echo "</select>";

echo "<div id = 'output'></div>";

?>

newtestx.php

<?php

$name = $_POST['name'];
echo $name."---";

?>

3 个解决方案

#1


1

You are sending a POST parameter with the key "select" to the server in your AJAX call:

您正在AJAX调用中向服务器发送带有“select”键的POST参数:

   data: { select: $(type+'[name='+theName+']').val()},

In newtestx.php you are trying to retrieve the value from a POST parameter with the key "name" - which doesn't exist:

在newtestx.php中,您尝试使用键“name”从POST参数中检索值 - 该值不存在:

$name = $_POST['name'];

You could fix this easily by giving the parameter keys the same name. If you would look for $name = $_POST['select'] the parameter would be found.

您可以通过为参数键指定相同名称来轻松解决此问题。如果您要查找$ name = $ _POST ['select'],则会找到该参数。

Inline Javascript is considered bad practice and there's no need to echo out the HTML markup, it makes the mark up harder to work with.

内联Javascript被认为是不好的做法,并且不需要回显HTML标记,这使得标记更难以使用。

newtest.php

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="[link to your javascript file here]"></script>
<select name='numbers' class="postValueOnChange" data-id-to-update="output" data-post-url="newtestx.php">
    <option value='1'>1</option>
    <option value='2'>2</option>
</select>
<div id='output'></div>

Javascript file

$(document).ready(function () {
    $('.postValueOnChange').change(postSelectedValue);

    function postSelectedValue(e) {
        var $self = $(this);
        var url = $self.data('post-url');
        var $elementToUpdate = $('#' + $self.data('id-to-update'));

        var jqxhr = $.ajax({
            type: "POST",
            url: url,
            data: {
                selected: $self.val()
            }
        });
        jqxhr.done(function (data) {
            $elementToUpdate.html(data);
        });
        jqxhr.fail(function (jqXHR, textStatus, errorThrown) {
            alert(errorThrown);
        });
    }
});

newtestx.php

<?php
$name = $_POST['selected'];
echo $name."---";
?>

更多相关文章

  1. 通过调用返回参数的本地函数来构建Ajax Data部分
  2. 删除一个HTML标记,但是保留innerHtml
  3. 在Paragraph标记中添加“名称”以使用POST
  4. 未捕获的ReferenceError:函数未定义,它标记
  5. 布局的标记建议和一些更一般的问题
  6. js去除html标记的测试问题。很多分!
  7. 当锚标记被单击时,角值从一个页面传递到另一个页面
  8. 如何为django模板中的标记创建动态id
  9. 如何在HTML标记上打印/显示动态值?

随机推荐

  1. Android android下的数据持久化和读取数
  2. Android(安卓)圆角图片的实现
  3. 【原创】Android 4.4前后版本读取图库图
  4. Android快速开发架构PlanA(一),船新版本的Ba
  5. 系出名门Android(1) - 在 Windows 下搭建
  6. Android公共库——图片缓存 网络缓存 下
  7. strut2服务器与android交互数据
  8. android调用输入软键盘回车键跟删除键
  9. Android(安卓)项目导入后真机运行提示:W/d
  10. 安卓selector使用方法