【问题标题】:Error ajax and PHP "Undefined index"错误 ajax 和 PHP“未定义索引”
【发布时间】:2016-01-07 06:20:26
【问题描述】:

我是 AJAX 新手,如果解决方案很明显,我很抱歉。 我必须在不刷新页面的情况下将日期发送到 MySQL 表,而我目前在执行此操作时遇到了问题。

代码给出 我出现以下错误:

" 注意:未定义索引:在 inserisci.php 第 6 行发布"

表格

 <form id="form" class="form-horizontal shadow-z-1" action="includes/inserisci.php" method="post">
        <fieldset>
            <legend style="text-align: center; position: relative; top: 8px;">Inserisci un Post</legend>
            <hr>
            <div class="form-group">
                <label for="inputTitle" class="col-lg-2 control-label">Titolo</label>
                <div class="col-lg-10">
                    <input type="text" class="form-control" id="inputText" name="titolo" placeholder="Scrivi qui il Titolo">
                </div>
            </div>
            <div class="form-group">
                <label for="textArea" class="col-lg-2 control-label" name="testo">Messaggio</label>
                <div class="col-lg-10">
                    <textarea id="messaggio"></textarea>
                </div>
                </div>

               <div class="form-group is-empty is-fileinput">
                    <label for="inputFile" class="col-md-2 control-label">File</label>

                    <div class="col-md-10">
                      <input type="text" readonly="" class="form-control" placeholder="Browse..." pmbx_context="19E61A0C-3526-4E51-8535-935982C4C335">
                      <input type="file" id="inputFile" multiple="" pmbx_context="1D2BCAEA-08CC-476A-8F4A-EF6BD51B9102">
                    </div>
                  <span class="material-input"></span></div>

            <div class="col-md-10 col-md-offset-2">
                      <button type="button" class="btn btn-default" onclick="document.getElementById('modalposta').style.display = 'none';">Cancel</button>
                      <button type="submit" class="btn btn-primary" onClick='send(); return false;' >Submit</button>
                    </div>
        </fieldset>
    </form>

INSERISCI.JS

  function send(){
var content = $.ajax({
        type: "POST",
        url: "index.php",
        data: {titolo:titolo & post:testo}           
  })
  .done(function() {
    alert( "success" );
  })                                                 
  .fail(function() {
    alert( "error" );
  })                                                
  .always(function() {
    alert( "complete" );
  });
}

INSERISCI.PHP

<?php

include('../core.php');

$titolo=$_REQUEST ['titolo'];
$post=$_REQUEST ['post'];

$sql = mysql_query("");

mysql_close();

?>

其他问题。 我需要在不刷新页面的情况下重新加载此内容。

INDEX.PHP

<?php $sql = mysql_query("")  or die ("Nessun errore");
    if (mysql_num_rows($sql) > 0)
    {


        while ($row = mysql_fetch_assoc($sql))
        {
            echo '

            <article class="flipper white-panel">
        <a class="flipcard flip" style="
        position: absolute;
        right: 0px;
        top: -10px;
        z-index:1;
    "><i class="material-icons" style="font-size: 40px;">bookmark</i></a>
        <div class="articolo">
        <img src="http://i.imgur.com/sDLIAZD.png" alt="">

            <h4><a href="#">'.$row ['titolo'].'</a></h4>
            <p>'.Markdown($row ['contenuto']).'</p>
             <div class="pull-right">
                            <span class="label label-default">alice</span>
                            <span class="label label-primary">story</span>
                            <span class="label label-success">blog</span>
                            <span class="label label-info">personal</span>
                            <span class="label label-warning">Warning</span>
                            <span class="label label-danger">Danger</span>
                          </div>
                          <hr>
                          </div>
        <div class="commenti">
            <ul class="comment-list">
                        <li>
                            <div class="comment-img">

                            </div>
                            <div class="comment-text">

                            </div>
                        </li>
                    </ul>
                    <input type="text" class="form-control inputcomment" placeholder="Lascia un Commento.."/>
                  </div>
      </article>
         ';
        }


    }

谢谢大家。

【问题讨论】:

    标签: php jquery ajax


    【解决方案1】:

    注意事项:

    1. 请不要使用mysql_* 函数。它们已被弃用。
    2. 您的代码容易受到 SQL 注入攻击。请确保您与他们联系。

    这是一个语法错误。这不是一个有效的 JavaScript 对象。将您的 data 对象更改为:

    data: {titolo:titolo, post:testo}
    

    您还需要在使用.done之前关闭$.ajax

      var content = $.ajax({
            type: "POST",
            url: "index.php",
            data: {titolo:titolo & post:testo}           // Should be closed here.
      })
      .done(function() {
        alert( "success" );
      })                                                 // Should not be closed. Remove ;
      .fail(function() {
        alert( "error" );
      })                                                 // Should not be closed. Remove ;
      .always(function() {
        alert( "complete" );
      });
      // });                                                Remove this!
    

    如果没有任何效果,请更改以下行:

    $titolo = $_POST['titolo'];
    $post = $_POST['post'];
    

    等等,你得到这两个的值了吗?

    var titolo = $('input[name="titilo"]').val();
    var testo = $('input[name="testo"]').val();
    

    【讨论】:

    • 同样的问题。未定义的索引:在第 6 行的 inserisci.php 中发布
    • 您没有关闭该行:$.ajax({ type: "POST", url: "index.php", data: {titolo:titolo &amp; post:testo}) .done.done 之前使用) 关闭它。
    • @Master-Antonio 有很多问题。请解决他们。请参阅上面的答案。
    • @Master-Antonio 你看到上面的答案了吗?
    • @Master-Antonio 再次更新以获得最终解决方案。
    【解决方案2】:

    这是因为您在 AJAX 请求中传递的数据不是有效的 javascript 对象,并且您没有从表单中获取变量:

    data: {titolo:titolo & post:testo}
    

    您应该改为:

    var titolo = $('form input[name="titolo"]').val();
    var testo = $('form input[name="testo"]').val();
    data: {
       titolo:titolo,
       post:testo
    }
    

    另外,作为一个旁注,当访问数组元素时,在变量和括号之间放置一个空格不是很好的形式。我很惊讶它甚至可以工作。

    $_REQUEST ['titolo']
    

    应该是

    $_REQUEST['titolo']
    

    【讨论】:

    • 你是对的。我只是在做一些事情,出于某种原因,这在我的脑海中。修好了。
    • 哈哈。而且,我无意中读到了这样的分享想法。 :) 无意冒犯。
    • 同样的问题。未定义的索引:在第 6 行的 inserisci.php 中发布
    • @Master-Antonio 我更新了我的答案。我没有意识到你没有先抓住变量。您正在发送一个 null 值,它被丢弃了。
    【解决方案3】:

    如果您想使用 ajax 表单提交,请使用

    更新您的表单定义
    <form id="form" class="form-horizontal shadow-z-1" action="#" method="post">
    

    使用以下更新的 javascript

    function send() {
        var content = $.ajax({
            type: "POST",
            url: "process.php",
            data: {
                titolo:  "Value for titolo",
                post: "Value for post"
            }
        }).done(function() {
            alert( "success" );
        }).fail(function() {
            alert( "error" );
        }).always(function() {
            alert( "complete" );
        });
    }
    

    【讨论】:

    • 相信这可能会导致 W3C 验证问题,action="#"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-25
    • 1970-01-01
    • 2018-01-25
    • 2011-02-21
    • 2016-08-17
    • 1970-01-01
    • 2015-08-18
    相关资源
    最近更新 更多