所以这个功能似乎让我感到困惑。
echo"
<td style='font-size:12px;width:150px;'><div style=\"overflow-y:auto; max-height:250px; width:200px;\">
{$row['Notes']} </div><br /><center><br />
<button onclick=\"myFunction('{$row['ID']}','$rowID')\">Add Note</button>
<form action=\"http://calls.fantomworks.com/functions/notes.php\" id='notesForm' name='notesForm' method='post'>
<input type='hidden' id='notesID' name='notesID' />
<input type='hidden' id='rowID' name='rowID'/>
<input type='hidden' id='notes' name='notes' />
</form>
</center>";
调用这个 javascript
<script language="JavaScript" type="text/javascript">
function myFunction(ID,rowID)
{
var x;
var ID = ID;
var rowID = rowID;
var note = prompt("Customer Note","Write your customer note here...");
if (note != null) {
document.getElementById("notes").value = note;
document.getElementById("notesID").value = ID;
document.getElementById("rowID").value = rowID;
document.getElementById("notesForm").submit();
}
else{
return false;
}
}
</script>
并在这个 php 页面结束
$notesID = $_POST['notesID'];
$rowID = $_POST['rowID'];
$note = $_POST['notes'];
//Redirect to browser
header("Location: ./index.php#row_$rowID");
唯一的问题是 rowID 似乎没有通过并生成以“index.php#row_”结尾的链接
我不明白为什么 rowID 没有通过,但 NotesID 和注释可以。 正如您从调试中看到的那样,该值就在那里。
感谢您的任何想法或建议!
最佳答案
"http://calls.fantomworks.com/index.php" 处的脚本正在由您的 javascript 函数发布 - 因此您寻求的变量应该通过 $_POST
全局可用。
尝试改变
header("Location: ./index.php#row_$rowID");
到
header("Location: ./index.php#row_{$_POST['rowID']}");
顺便说一句,您在 javascript 函数中定义的三个变量似乎是多余的,并且可以通过事物的外观将其删除,即:-
var x;
var ID = ID;
var rowID = rowID;
自从发布原始内容(并且没有注意到@OP 对发布的变量的分配)后仔细查看 - 有问题的页面上有数百个表单 - 行与行之间使用相同的 IDS。恕我直言 - 这绝对不是前进的方向 - 当您通过单击按钮动态设置值时,您可以只有一种形式的“添加注释”。看起来相关的变量(rowID 等)正在设置并分配给调用 javascript 的按钮,因此理论上您可以只有一种用于发布到“notes.php”的表单,但每一行都有这个按钮。
一般性的批评/建议
页面加载速度非常慢 - 部分原因是有数百个复杂的表格行布局,并且从外观上看每个按钮都有一个表单 - 然后是图像本身是全尺寸的但实际上可能是(应该是)缩略图。如果每个按钮都像上面问题中的那样动态分配变量,则可以大大减少表单的数量。
关于javascript - 问题通过 Javascript 传递 PHP 变量并返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32523662/