需要帮助Ajax

时间:2018-04-18 16:21:45

标签: php ajax post

我将此代码赋予main.php的POST值,使用onclick函数将调用UserID,并且应根据选定的值更改user_id值,并在{{1}中选择加载user_id }}

user_profile.php?ID=user_id

成功发布后,警报显示所选值,表示var USER_ID = 0; function UserID(user_id) { USER_ID = user_id; $.ajax({ type: "POST", url: "main.php", data: {ID: USER_ID}, success: function(msg) { alert(USER_ID); $('#user_profile_content').load('user_profile.php?ID=USER_ID #user_profile_content'); }, error: function() { alert("error"); } }); } 成功,但我不知道如何根据收到的值更改POST。它不是替换全局变量user_profile.php?ID=USER_ID

1 个答案:

答案 0 :(得分:0)

From what I understand you want to change the url route, try:

...
success: function(msg){
    window.location.href = `user_profile.php?${msg.id}`
    $('#user_profile_content').load(`user_profile.php?ID=${msg.id}
#user_profile_content`);
...
  1. ES6 template literals allow you to insert the returned value in a string.

  2. Window.location.href allows you to change the route on the browser (Frontend), I recommend you do it from the Backend if possible though.

  3. This answer assumes msg.id is the desired value that you want to pass since the contents of the response are not specified.