-1

I have this code to POST value to main.php, with onclick function UserID will be called and value which is user_id should be changed based on selected value and load selected user_id in 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");
                  }
              });
}

After success post, alert is showing with selected value, it means POST was successful but I don't know how to make user_profile.php?ID=USER_ID to be changed based on received value. It's not replace global variable USER_ID.

halfer
  • 19,824
  • 17
  • 99
  • 186
Aqil
  • 104
  • 10
  • 1
    so in main.php you return something ? – Amr Aly Apr 18 '18 at 16:32
  • 1
    Please post the snippet of the returned values in the ajax – andrralv Apr 18 '18 at 16:33
  • Check [this link](https://stackoverflow.com/questions/824349/modify-the-url-without-reloading-the-page?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa). It seems related. – xuhaib Apr 18 '18 at 16:34
  • show us the code for the main.php – Lelio Faieta Apr 18 '18 at 16:36
  • Use a descriptive headline. "Need Help" applies to _every question on SO_ – random_user_name Apr 18 '18 at 16:58
  • @AmrAly , i edited the code, please check – Aqil Apr 18 '18 at 17:02
  • if you want to access `USER_ID` inside your `success` block you can do some [string interpolation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) like so: `$('#user_profile_content').load('user_profile.php?ID=${USER_ID} #user_profile_content');` – Amr Aly Apr 18 '18 at 17:23
  • 1
    @AmrAly not worked,in this case it works only `$('#user_profile_content').load('user_profile.php?ID='+USER_ID);` but don't know how to replace #user_profile_content – Aqil Apr 18 '18 at 18:05
  • @Sara you're right i should have replaced the `` with the ''. – Amr Aly Apr 18 '18 at 18:12
  • @AmrAly there is another thing,how can i make php variable of returned value? – Aqil Apr 18 '18 at 18:46
  • I am afraid i don't understand your question could you please be specific on what you want to achieve – Amr Aly Apr 18 '18 at 19:33

1 Answers1

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.

andrralv
  • 810
  • 7
  • 18
  • in this case `$('#user_profile_content').load('user_profile.php?ID='+USER_ID);` works only,but don't know how to load `#user_profile_content` instead the first one – Aqil Apr 18 '18 at 18:06