【问题标题】:php/JS form questionphp/JS 表单问题
【发布时间】:2011-06-22 22:47:15
【问题描述】:

我是 php 和 JS 的新手,我有这段代码一直卡在我无法开始工作:

我的 something.php 只有$variable= $_POST['SUBMIT'] echo $variable

<?php
$list=$_POST['added'];
?>

<html> 
<head> 
  <meta http-equiv="content-type" content="text/html; charset=UTF-8"> 

  <script type='text/javascript' src='http://code.jquery.com/jquery-1.6.js'></script> 

  <link rel="stylesheet" type="text/css" href="/css/normalize.css"> 
  <link rel="stylesheet" type="text/css" href="/css/result-light.css"> 

  <style type='text/css'> 


#display{
    width: 500px;
    height: 250px;
    overflow: scroll;
    background-color:white;
}

#display div {
    clear: both;
}
#display div span{
    float: left;
    padding: 10px;
}

.edit {
    color: blue;
}

.delete {
    color: red;
}



   body {background-color:orange;}
p {color:orange;
   size=3;
}

  body {

  background-image: url("http://dev.icalapp.rogersdigitalmedia.com.rogers-test.com/rogers_blackberry_8900.jpg");

  background-position: 50% 50%;

  background-repeat: repeat;

}

  </style> 

  <script type='text/javascript'> 
  //<![CDATA[ 
  $(window).load(function(){
  $('#add').click(function() {
    this.innerHTML = 'Add';
    var input = $('#addInput');
    var display = $('#display');
    var form = $('#addForm');
    if ($.trim(input.val()).length > 0) { //is there input?
        var div = $('<div>').append($('<span>', {
            text: input.val()
        }), $('<span>', {
            text: 'delete',
            class: 'delete'
        }), $('<span>', {
            text: 'edit',
            class: 'edit'
        }))
        display.append(div);
        form.append($('<input>', {
            value: input.val(),
            type: 'hidden',
            name: 'added[]'
        }));
        input.val('');
        }
    })



    $('.edit').live('click', function(){
        $('#add').html('Edit');
        var input = $('#addInput');
        var index = $(this).parent().index() + 1;
        input.val($(this).parent().children().get(0).innerHTML); //show value
        $(this).parent().remove(); //remove it from the list
        $('#addForm').children().eq(index).remove();
    });

    $('.delete').live('click', function(){
        var index = $(this).parent().index() + 1;
        $('#addForm').children().eq(index).remove();
        $(this).parent().remove(); //remove it from the list
    });




  });


  //]]> 
  </script> 

  <title>Admin Phone Setup</title>
</head> 
<body> 

<STYLE>H2 {FONT-SIZE: 21pt; COLOR: #ff3399}; </STYLE>
<CENTER>
<H2>Twilio Admin Setup</H2>
</CENTER>


<p id="demo">Enter Phone Numbers</p>
  <input id="addInput"/> <button id="add">Add</button> 
<div id='display'></div> 
<form action="something.php" id="addForm"> 
    <input type="submit" value="SUBMIT"/> 
</form>   
</body> 
</html>

我想要做的是,一旦有人将一堆数字添加到该列表中并单击提交,它应该将这些值发送到 something.php,以便我可以使用这些值...我真的被卡住了,不知道是什么我做错了...我认为这可能是我的显示列表的变量或问题。

【问题讨论】:

  • 一目了然,我的建议是尝试将输入放在
    标签内,因为提交表单只会发送标签内指定的输入。
  • 并且由于您的 php 文件查找 _POST,因此在打开表单时添加 method="post"(即)
  • 您的 PHP 文件似乎正在寻找名为 SUBMIT 的输入,但不存在这样的输入...
  • 值是通过数组获取的,所以提交后使用数组值获取值。 $_POST['added'][]。在此之前不要忘记在表单标签中添加方法属性。

标签: php javascript jquery list


【解决方案1】:

1) 您的按钮未命名。要获取按钮值,您必须分配名称属性,例如

&lt;input type="submit" name="mysubmitbutton" value="SUBMIT" /&gt;

2) 在您的 something.php 中,您使用的是 POST 数组,但您的表单是使用 GET 数据提交的,因为您的表单标签上没有“方法”属性。此外,您似乎尝试通过将按钮的值用作索引来访问按钮值(这不是单词;))。您可以选择(在命名按钮后) a) 在你的 PHP 脚本中使用 $_GET['mysubmitbutton'] b) 将您的表单标签更改为

&lt;form ... method="post" ... &gt;
并使用 $_POST['mysubmitbutton']。

【讨论】:

  • 我已经修复了它,但现在的问题是当我点击提交按钮时它显示“提交”......所以 something.php 显示提交......它应该显示我的数字列表添加到列表中..
  • 遍历你的 add 数组并回显这些项目。例如foreach($_POST['add'] as $item) {echo $item . '
    '; }
猜你喜欢
  • 2010-10-27
  • 1970-01-01
  • 2023-03-25
  • 2012-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-01
  • 2015-01-02
相关资源
最近更新 更多