0

There is a similar question, it is 7 years old and not very helpful for my situation. Didn't want to frustrate anyone into thinking I didn't search. Everything else I've searched for has dealt with forms, which isn't what I need. I have the C# background and am not very familiar with PHP, so bear with me.

I have a forum hosted by another service, this service has their own database and such for storing users. This service doesn't support a function I'm trying to incorporate so I'm attempting to host my own database and do it myself.

Here's the current situation:

  • Forum database holds user information, such as UserIDs, to which are unique to the users. I want my database to correspond.
  • I need to send the logged in user's ID to my database, without putting in a form and asking them to give me their user ID.
  • I have access to the variable that holds their ID, and just need to know how to pass it over. I can't change the forum to a.PHP and just send it from the main page.

My biggest problem:

  • Most of the information I've found is outdated/depreciated.
  • I know JS/AJAX can be used for this, but I haven't seen any examples other than for form and I have no idea how to change it to just sending the already populated variable to my PHP file that will upload it to my database.

    var id= '<!--|id|-->';
    window.location.href = "myphpfile.php?id=" + id; 
    

This is as close as I've gotten, it does what I want, but it takes my users away from the forum in order to submit this. Is there a way of passing it to the file without needing to redirect the page? Any help is appreciated, especially if you can explain to me how it works/why it works.

Serving Quarantine period
  • 66,345
  • 10
  • 43
  • 85
  • use CURL/session? – Wils Apr 18 '18 at 06:05
  • _"Did want to frustrate anyone"_ - I hope you meant _"Didn't"_ ;-) – Magnus Eriksson Apr 18 '18 at 06:06
  • You can use `SESSION` concept – Serving Quarantine period Apr 18 '18 at 06:07
  • I think this is a good question. – Wils Apr 18 '18 at 06:07
  • do you know how to get element by id, class or name so there is no need form you can use ajax easily there. – Nishit Manjarawala Apr 18 '18 at 06:07
  • 1
    well, ajax don't need form actually. – Wils Apr 18 '18 at 06:08
  • If you are able to create a page that uses the "other service", then using AJAX to send the data to a PHP script should be very easy. Collect the data into JS vars, then make the AJAX call. In your PHP script, you get the data send as either $_POST or $_GET (depending on how you sent the data) and save to your DB. – Sloan Thrasher Apr 18 '18 at 06:11
  • 2
    @Wils could you elaborate please how it's good question – Rahul Shrivastava Apr 18 '18 at 06:11
  • First rule of programming: First understand, then write code. You should not try to understand while coding. – Aniket Sahrawat Apr 18 '18 at 06:15
  • _"I know JS/AJAX can be used for this, but I haven't seen any examples other than for form"_ - Then you can't really have done that much research. – Magnus Eriksson Apr 18 '18 at 06:18
  • @MagnusEriksson I've looked for an option that works for me for several hours; apologies if that wasn't enough for you. It wasn't my intention to upset anyone. –  Apr 18 '18 at 06:26
  • @AniketSahrawat I didn't see much of a point in learning an entire language for this one thing. I'm not a front-end developer, just trying to fix something since no solutions are avaliable for said platform. –  Apr 18 '18 at 06:28
  • I'm simply pointing out that if I just google on "make ajax request", none of the examples in the top pages uses a form. – Magnus Eriksson Apr 18 '18 at 06:29
  • I'm sorry, I didn't know what an 'ajax request' was. All I knew was that people were suggesting that language for use with posting form fields, but I didn't see anything that looked like it would work. I'm not really sure how to avoid that in the future as its a bit difficult to Google something you're not aware of, but I'll try to avoid it in the future. Thanks for pointing it out. –  Apr 18 '18 at 06:33
  • I can't delete this question, if anyone else wants to they're welcome to do so. Apologies that it wasn't a great question! :) –  Apr 18 '18 at 07:06

2 Answers2

0

Do you have jQuery? Since you have your PHP file that writes to your DB as a separate file, you can use $.post from that page to call that file.

 $.post('path/to/phpfile.php', {loginId: <put the login id here>})
    .done( function(data) {
            <if you wanna do something once the operation is done>
    });

Then on the PHP file, you can grab it via $_POST['loginId']. Then do your processing there, and echo something back if you want.


Or if you want the vanilla js method you can refer to this question answered here: Send POST data using XMLHttpRequest

Lew Keyang
  • 11
  • 5
  • Since we don't know if the OP uses jQuery or not, it's better to just give an answer in vanilla JS. It's simple enough and the answer would be more helpful for a broader audience. – Magnus Eriksson Apr 18 '18 at 06:16
  • so you want a primitive AJAX function? – Wils Apr 18 '18 at 06:19
0

Well, If I understood you you could make it like this:

Lets say the User_ID is in $_SESSION['id'] you could just:

$.ajax({

    url : 'yourphpscript.php',
    type : 'POST', //If it wants a POST, you may change it to GET
    data : {
        'id' : <?php echo $_SESSION['id']; ?> //or where ever your variable is
    },
    success : function(data) {              
        //Do something on success
    },
    error : function(request,error)
    {
        //Do something on error
    }
});

Just place this with an onload, you should be fine :)

EDIT: But be aware that Users could start sending fake data, that may be or not be a problem

Altay Akkus
  • 248
  • 1
  • 10
  • 1
    It was my understanding that you couldn't use PHP scripts inside of an HTML page; is that incorrect? –  Apr 18 '18 at 06:29
  • yep, but your forum has to have php enabled on the sites, how else should it check if its logged in :) – Altay Akkus Apr 18 '18 at 06:31
  • I honestly assumed it was JavaScript; thanks so much! I'll see if this works! –  Apr 18 '18 at 06:34
  • `session_start();` is worth mentioning. – Aniket Sahrawat Apr 18 '18 at 06:47
  • Most websites (atleast mine :D) have an session start and an login check at the very first lines of the document. So I just assumed it was already set. @MalloreeEady well, you apaprently know how to get the Variable of the user_id, how do you get it then :D – Altay Akkus Apr 18 '18 at 07:04