php - ajax 和 PHP 错误 "Undefined index"

标签 php jquery ajax

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

代码给出 我遇到以下错误:

" Notice: Undefined index: post in inserisci.php on line 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>
         ';
        }


    }

谢谢大家。

最佳答案

Notes:

  1. Please do not use mysql_* functions. They are deprecated.
  2. Your code is vulnerable to SQL Injection Attacks. Please make sure you address them.

这是一个语法错误。这不是一个有效的 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();

关于php - ajax 和 PHP 错误 "Undefined index",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34361737/

相关文章:

ajax - Spring3 将 XML 解码为 Java

php - 实时 AJAX 搜索手机号码

javascript - 创建上一页按钮

php - 如何在单个查询中计算多个列和字段的数量

php - 在数组中查找相似的单词并消除它们

javascript - 用按键代替鼠标点击

javascript - 触摸事件后移动 div

php - 如何使用比根更深的 PHP DOM 向 XML 添加新元素?

jquery - 使用 JQuery 悬停时触发 CSS3 动画不起作用

javascript - XPage ajax 文档创建