Jacques1 Posted August 20, 2014 Share Posted August 20, 2014 I'm not sure if it helps to start a performance discussion now. This may be a valid reason for using Ajax, but right now, I think we're dealing with more basic problems. When I do an ajax process, I use js to make the call, but it is calling a PHP script which returns a "string". WTH is a 'javascript string'? And (as mentioned by others in this conversation) how is receiving this string any different than pasting it into the code like I'm doing now? The difference is how the data gets from PHP to JavaScript. Your way is the spaghetti code way: You have PHP code within JavaScript code within HTML markup, and you hope that your PHP output will eventually end up as a proper JavaScript object and neither confuse the HTML interpreter nor the JavaScript interpreter. As you've experienced multiple times, this doesn't work very well. You get strange backslashes, you get syntax errors and whatnot. It's time to draw the right conclusions: This approach is shit. So as you throw away the spaghetti code, you now need a way to fetch the data from JavaScript itself. And that's where Ajax comes into play. Yes I use prepared queries [...] But do you also understand why you're using them? That's the key to the problem here. Quote Link to comment https://forums.phpfreaks.com/topic/290519-encoding-a-pathname/page/2/#findComment-1488421 Share on other sites More sharing options...
ginerjm Posted August 20, 2014 Author Share Posted August 20, 2014 Still not getting anywhere here. So I make an ajax call to get php to gather my data as it currently does. Then what? What do I echo out for my js to then handle? Quote Link to comment https://forums.phpfreaks.com/topic/290519-encoding-a-pathname/page/2/#findComment-1488424 Share on other sites More sharing options...
Jacques1 Posted August 20, 2014 Share Posted August 20, 2014 You don't echo anything. That's the point. The Ajax request loads all your JSON data into a JavaScript object, and that's it. Now you can use your image paths. I think the problem is that you seem to be rather inexperienced with JavaScript. I don't mean this offensive, it's just my impression. I recommend that you take a few minutes of your time to learn to basics of jQuery (it's very simple) and then try things out yourself. Quote Link to comment https://forums.phpfreaks.com/topic/290519-encoding-a-pathname/page/2/#findComment-1488428 Share on other sites More sharing options...
ginerjm Posted August 20, 2014 Author Share Posted August 20, 2014 I actually am pretty good at JS - have written a lot of it. I just don't use json at all and have no idea how a php script called via ajax is going to return the data I need. Last time I'll ask. Can you Tell me what my ajax script s/b returning? You seem to be avoiding giving me that detail. Quote Link to comment https://forums.phpfreaks.com/topic/290519-encoding-a-pathname/page/2/#findComment-1488454 Share on other sites More sharing options...
Jacques1 Posted August 20, 2014 Share Posted August 20, 2014 Last time I'll ask. Can you Tell me what my ajax script s/b returning? You seem to be avoiding giving me that detail. I wrote down the exact PHP code in #17. Didn't you see that? The PHP script simply prints the JSON-encoded data: echo json_encode($data); It should also set the appropriate Content-Type header. The MIME type of JSON is application/json. So JavaScript makes an Ajax request to this PHP script. It gets back some JSON-encoded data. After it has decoded the response with JSON.decode(), it has a JavaScript object with all the data. And that was the point of this exercise. Quote Link to comment https://forums.phpfreaks.com/topic/290519-encoding-a-pathname/page/2/#findComment-1488461 Share on other sites More sharing options...
ginerjm Posted August 21, 2014 Author Share Posted August 21, 2014 Jacques1 - Thank you for giving me something I can work with. I will try this later as even us retirees have things to do besides code all day. ps - hoping that 'JSON.decode()' is a pure JS thing since I still haven't learned JQ and will most likely not do it soon. Do you have a recommended book for learning this? Quote Link to comment https://forums.phpfreaks.com/topic/290519-encoding-a-pathname/page/2/#findComment-1488493 Share on other sites More sharing options...
Jacques1 Posted August 21, 2014 Share Posted August 21, 2014 JSON.decode() is native JavaScript. Note, however, that some ancient browsers like IE 7 don't support it. If your site should run on those as well, you (again) need jQuery or some external JSON library. jQuery is a very lightweight and clear framework, most people pick it up intuitively without having to read any books. If you go through the basics and keep the documentation handy, you should be able to switch almost immediately. Quote Link to comment https://forums.phpfreaks.com/topic/290519-encoding-a-pathname/page/2/#findComment-1488506 Share on other sites More sharing options...
ginerjm Posted August 21, 2014 Author Share Posted August 21, 2014 And --- Thank You! Quote Link to comment https://forums.phpfreaks.com/topic/290519-encoding-a-pathname/page/2/#findComment-1488511 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.