[英]Undefined index when defining PHP POST inside JQuery variable
好的,所以當人們單擊左側的注釋名稱時,我將使用Ajax在屏幕上顯示注釋,注釋將顯示在右側。
現在,當用戶編輯便箋時,他們應該能夠單擊一個按鈕,並將新文本保存回原來加載該便箋的ID。
基本上,我可以在PHP中獲取注釋ID,但隨后如何將該ID傳遞給jquery,然后使用。
我的劇本:
<?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()'> </div>" . $data['NoteName'] . " <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 + '¬etext1=' + 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>
如您所見,即時消息試圖將函數放在相同的php文件中,因此我可以直接使用$ _POST ['noteid']來保存我想要的ID。
那么,為什么要說在我的函數中,當索引頂部的ID上有一個“ isset”並檢索ID卻沒有問題時,該索引卻未定義?
你可以這樣使用...
var noteid = <?php if(isset$_POST['noteid']){ echo $_POST['noteid'];} ?>;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.