【发布时间】:2021-02-16 03:33:27
【问题描述】:
假设我有一个带有 onclick 功能的元素,它在单击时会触发 prompt()。 一旦用户填写了提示,它还会触发一个 ajax 请求,该请求会将带有 post 类型的提示值发送到 php 页面以获得响应。
所以我有这个代码:
<i class="fas fa-wrench text-warning editmode" id="Pseudo" onclick="goEdit(this.id)" title="Edit my pseudo"></i>
function goEdit(id) {
console.log(id); // The console log here print me "Pseudo" and that's what i want
$(document).ready(function() {
let userchange = prompt();
$.ajax({
type: 'POST',
url: 'traitements/traitement-profil.php',
data: {
id: userchange
// The problem is here ! I want to send a $_POST['Pseudo'] that contains the answer of the prompt.
// But that doesn't work.
// Instead if i a do a print($_POST) in my php page i get this as a response in my alert below:
// array(1) { ["id"]=> array(1) { ["id"]=> string(24) "Answer of the prompt here"
},
dataType: 'text',
success: function(data) {
alert(data);
}
});
});
}
我需要我的 $_POST 等于所单击按钮的 id 的值的原因是因为我需要在同一页面上有多个像这样的按钮,每个按钮都有自己的用途,但总是做同样的事情。只有 $_post 参数会改变每个参数,这个参数必须等于点击按钮的 id
【问题讨论】:
标签: javascript php jquery ajax post