简体   繁体   中英

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.

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.

Basically i can get the note id in PHP but then how do i pass that ID to jquery to then use.

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.

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?

你可以这样使用...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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