Jump to content

encoding a pathname


ginerjm

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.