-9

What is the best way to "convert" a JS variable into one usable in PHP?

I understand that it is not as easy as that due to one being on the client and one being on the server, but I need to pass bit of data to a PHP function so that it can be inserted into the DB.

variable name in JS is srcID

I want to insert this into the avatar column of my table users where the username matches the _SESSION['user]

<form method="POST" action="saveAvatar.php">
         <button type="submit">This one please!</button>
</form>
Francesca
  • 22,178
  • 26
  • 80
  • 145
  • @BenjaminGruenbaum I looked at that but it seems to have so much more functionality than is required. I have yet to find an answer here on SO that doesn't have a tonne of extra uneeded code. – Francesca Mar 28 '14 at 22:37
  • YOu could refer to this answer http://stackoverflow.com/questions/6095531/passing-javascript-variable-to-php – marcovisona Mar 28 '14 at 22:38
  • @Francesca you need to understand the concept. Grasp how the HTTP protocol works, what is an HTTP request, how PHP serves pages. What is the difference between client side and server side and how data is passed between the two. Trust me, you don't need to 'get a short code to do it', you need to understand how the internet works - otherwise you'll just end up with a huge pile of code you don't understand. – Benjamin Gruenbaum Mar 28 '14 at 22:39
  • I'm comfortable with "how the internet works" but thanks for talking to me like a child anyway. – Francesca Mar 28 '14 at 22:40
  • @Francesca I'm sorry if that came out as condescending, I'm genuinely trying to help you here. When I'm suggesting looking into the HTTP protocol and how the internet works I'm trying to help you not fall into a pit many of us web developers were in once in a while. You can take my advice or leave it but I genuinely feel like this can help you out a lot more than a short code snippet. – Benjamin Gruenbaum Mar 28 '14 at 22:42
  • I wasn't looking for a short code "snippet" as you say, I already read the answer that you posted before I made this question. I was simply asking for a basic solution, not something that added a lot of extra functionality. I also don't want to use an AJAX request. – Francesca Mar 28 '14 at 22:44
  • Perhaps the problem is that the question is not well defined. The answers that others have suggested vary a lot - some are about AJAX, some are simple form submissions - but I'm not sure from the question what approach you're taking. – Kryten Mar 28 '14 at 22:46
  • @Francesca - Since your question is not 100% clear exactly what you are are trying to do, can you please tell me if the references I've provided in the answer below is what you are looking for? – jfriend00 Mar 28 '14 at 22:52

1 Answers1

0

If you're OK with changing the current browser page, then you can either do a form submission (where the form elements will contain the data to be sent to the server) or you can just create an URL with all the data encoded in the query parameter on the URL and set window.location = newURL;. That will send that GET request to the server, the server can process off the query parameters and then decide what result to show the user on the new page.

Assuming, you don't to change the current browser page, then from the client, you call the server with an ajax function written in javascript and pass the data with the ajax call. On the server, you field that request, extract the data from the request and process it into your database.

If you want examples, there are lots of examples of code on the web. Just search for "ajax javascript" and there are thousands of examples. I'm looking for a good representative StackOverflow answer to point you to or mark your question a dup of, but haven't found one yet so I'm steering you in the right direction with this answer for now.

Read here first on MDN: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest as a good general reference on Ajax.


Then, here's a simple function in plain javascript for making an Ajax call:

How to make an AJAX call without jQuery?


There here's are several simple plain javascript references on Ajax:

http://www.degraeve.com/reference/simple-ajax-example.php

http://www.htmlgoodies.com/beyond/javascript/article.php/3767776

http://www.ryannedolan.info/teaching/cs4830/examples/ajax-examples

http://www.javascriptkit.com/dhtmltutors/ajaxgetpost.shtml


Though you didn't mention a cross-browser library in your question, many libraries just as jQuery include a lot of helpful code for managing AJAX calls.

Community
  • 1
  • 1
jfriend00
  • 580,699
  • 78
  • 809
  • 825
  • Not the downvoter by the way, it's a pretty correct answer. Just expected more from you. – Benjamin Gruenbaum Mar 28 '14 at 22:36
  • I'm looking for a good up, haven't found one yet. Geez. Why didn't you find a dup if there are so many. – jfriend00 Mar 28 '14 at 22:36
  • Or you just set a simple anchor with a parameter in the request query… No need for AJAX here – feeela Mar 28 '14 at 22:36
  • Here @jfriend00 http://stackoverflow.com/questions/1917576/how-to-pass-javascript-variables-to-php – Benjamin Gruenbaum Mar 28 '14 at 22:36
  • 2
    Or here http://stackoverflow.com/questions/8191124/send-javascript-variable-to-php-variable or here http://stackoverflow.com/questions/6095531/passing-javascript-variable-to-php or http://stackoverflow.com/questions/9882211/pass-javascript-variable-to-php-post or http://stackoverflow.com/questions/133925/javascript-post-request-like-a-form-submit – Benjamin Gruenbaum Mar 28 '14 at 22:37
  • So, why aren't all you guys voting to close the question as a dup if you think all those are such perfect dups? As of this moment, NOBODY has voted to close the OP's question as a dup. I am looking at the references to see if one is a decent dup. Haven't found one yet. – jfriend00 Mar 28 '14 at 22:39
  • We are, it takes time :) All I said is that you have some good answers and knowledge and that I was disappointed to see you answer this - that's all. – Benjamin Gruenbaum Mar 28 '14 at 22:40
  • @BenjaminGruenbaum - You need to find a plain javascript answer that actually answers the question. I'm assuming the OP wants to stay on the same page and send the data, so form submits or `window.location` answers don't seem appropriate. None of your references looked like a good dup to me. I am still looking. – jfriend00 Mar 28 '14 at 22:42
  • OK guys, I've spent 15 minutes looking for the quinteseential duplication on StackOverflow that shows the OP how to solve their problem with an Ajax call and I didn't find a particularly good one. There are examples of Ajax calls, but not an answers that actually explain what the OP's different options are for getting data from client to server. So, I explained the options here in my answer and gave the OP a bunch of references. If you find a good duplicate reference, post a comment with a link here (and vote to close the OP's question as a dup of it) so I can take a look. – jfriend00 Mar 28 '14 at 23:01