duanjianqu3685 2016-01-02 14:02
浏览 48
已采纳

AJAX调用中缺少POST参数会导致PHP脚本中出现未定义的索引警告

I am using AJAX for the first time so I write a code and I followed a tutorial, but when I try to send my post with ajax I am getting undefined index and I really don´t know why, I tried to search answer hear but since I am using AJAX and javascript for the first time, the code there didn´t tell me anything. Here is my code, I would be really greatful for any help, thank you.

js method

function post() {
    var name = $('meno').val();
    var priez = $('priezvisko').val();
    $.post( "aktualizuj.php", {
        'meno': name,
        'priezvisko': priez
    },
        function (data) {
        $('#result').html(data);
    }
        );
}

html form...

<html>
<head>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script type="text/javascript" src="prihlasenie.js"></script>
</head>
<body>
<form>
    <label for="meno" >Meno:</label><input type ="text" name="meno" id="meno" value ="meno" ><br>

    <label for="priezvisko" >Priezvisko:</label><input type ="text" id="priezvisko" name="priezvisko" value ="priezvisko" ><br>
<input type="button" value="ulozZmeny"  onclick="post()" >
</form>
<div id="result"></div>
</body>
</html>

this where I should get from ajax/javascript

session_start();
require "pripojenie.php";


    $meno = $_POST['meno'];

    $priezvisko = $_POST["priezvisko"];
    $login = $_SESSION['login'];
  • 写回答

1条回答 默认 最新

  • doumen9709 2016-01-02 14:17
    关注

    jquery doesn't serialize key:value pairs where value is undefined, i.e.

    $.post(url, { foo:undefined });
    

    results in jquery not sending any POST parameter.

    The problem is because of the following lines,

    var name = $('meno').val();
    var priez = $('priezvisko').val();
    

    This would look for an element meno and an element priezvisko (and then their values) as if you had a document like

    <p>
      <meno>...</meno>
      <priezvisko>...</priezvisko>
    </p>
    

    But you're looking for elements that have meno/priezvisko as value of their id attribute(s):

    var name = $('#meno').val();
    var priez = $('#priezvisko').val();
    

    You should nevertheless check the existince of the parameters in your php script. Nothing prevents another script/bot/user to invoke your script with different parameters or no parameters at all.
    see also:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度