简体   繁体   English

在JQuery变量中定义PHP POST时的未定义索引

[英]Undefined index when defining PHP POST inside JQuery variable

Ok so i am using Ajax to display a note on the screen when people click on a note name on the left and the note will show on the right. 好的,所以当人们单击左侧的注释名称时,我将使用Ajax在屏幕上显示注释,注释将显示在右侧。

Now when a user edits the note they should be able to click a button and it saves the new text back to the ID the note was originally loaded from. 现在,当用户编辑便笺时,他们应该能够单击一个按钮,并将新文本保存回原来加载该便笺的ID。

Basically i can get the note id in PHP but then how do i pass that ID to jquery to then use. 基本上,我可以在PHP中获取注释ID,但随后如何将该ID传递给jquery,然后使用。

My script: 我的剧本:

<?php include 'connectionDetails.php'; ?>

<?php

    if (isset($_POST['noteid'])) 
    {
        $showNoteInfo = "SELECT Note, NoteName FROM Notes WHERE NoteID = " . $_POST['noteid'];
        $stmt = sqlsrv_query($conn, $showNoteInfo);
    }

    if (isset($_POST['noteid'])) 
    {
        if (empty($_POST['noteid'])) 
        {
            $notes = 'No Data';
        }
        if (sqlsrv_has_rows($stmt)) 
        {
            $data = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC);

            echo "<div class='custom-font title-container'>
                    <div class='expand-button-container fa fa-expand' onclick='expandWindow()'></div>
                    <div id='title-container1'><div class='edit-note fa fa-pencil' onclick='editGeneralNote()'>&nbsp;&nbsp;&nbsp;</div>" . $data['NoteName'] . "&nbsp;<div class='save-note fa fa-thumbs-up' onclick='saveGeneralNote(); submitNoteText();'></div></div>
                  </div>";
            echo "<textarea spellcheck='false' readonly id='ta1'>" . $data['Note'] . "</textarea>";
        } 
        else 
        {
            echo "No data found";
        }
    }
?>

<script type="text/javascript">

    // Submit generalNote to the database

function submitNoteText()
{
    var noteid = <?php echo $_POST['noteid']; ?>;
    var notetext = $("#ta1").val();

    var dataString = 'noteid1=' + noteid + '&notetext1=' + notetext;

    console.log("NoteID: " + noteid);

    if(noteid == ''||notetext == '')
    {
        alert("NoteID or Text is blank");
    }
    else
    {
        $.ajax({
            type: "POST",
            url: "phpFiles_Notes/submitNoteText.php",
            data: dataString,
            cache: false,
            success: function(result){
                alert(result);
            }
        });
    }
    return false;
};

</script>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript" src="scripts/scripts.js"></script>

As you can see im trying to put the function in the same php file so i can directly use $_POST['noteid'] which holds the id i want. 如您所见,即时消息试图将函数放在相同的php文件中,因此我可以直接使用$ _POST ['noteid']来保存我想要的ID。

So why is it saying that in my function the Index is Undefined when at the top it has an 'isset' on the id and retrieves the ID no problem? 那么,为什么要说在我的函数中,当索引顶部的ID上有一个“ isset”并检索ID却没有问题时,该索引却未定义?

你可以这样使用...

var noteid = <?php if(isset$_POST['noteid']){ echo $_POST['noteid'];} ?>;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM