Jump to content

escaping strings with dbl and sgl quotes


ginerjm

Recommended Posts

I've got a file with some strings that have both types of quotes in them.  And I seem to have managed to get the data, display it in my html, store it in a js array (using a json_encode in php and then simply inserting it into my js) but I cannot seem to pass the string as a parameter form an onclick function call to js. 

 

For most strings the addslashes makes it work in the function call.  But for those with both sets of quotes it won't work.  My console tells me there are "unterminated string constants..".  I've experimented with many silly changes but none make it work.

 

Ex. of the strings:

 

What do you mean "It's crooked"?

 

Of course I could remove the contraction and that would probably work, but that would be a hack, would not it?

Link to comment
Share on other sites

We need to see your code.

 

However, it sounds like you're always running into the same problem: You try to inject some value from one language to another, you don't know how to prepare the value propely, and so the whole thing blows up. You already had that issue with the image paths, now you have it with some event attributes, and I fear you'll have it again in the near future.

 

Maybe it's time to rethink your programming style. The thing is: Mixing different languages and injecting values from one language into the code of another language has always been a bad idea. It works to some extend as long as you're doing simple tasks with simple code. But appearently you're past this stage.

 

A more modern approach is to avoid this PHPHTMLJavaScript inline scripting stuff altogether and put all JavaScript code into static external files. Instead of stuffing some PHP string into some onclick attribute, you fetch the data with JavaScript (using Ajax for example) and then assign an event handler to the button.

 

For example:

<!DOCTYPE HTML>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>JSON test</title>
        <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
        <!-- Put this into an external script -->
        <script>
            $(function () {
                // load the data with Ajax
                $.getJSON('image_data.php', function (image_data) {
                    // assign an event handler to the button; the handler has access to the data we've just retrieved
                    $('#some-button').click(function () {
                        alert('See the console for the data.');
                        console.log(image_data);
                    });
                });
            });
        </script>
    </head>
    <body>
        <button id="some-button">Click me</button>
    </body>
</html>
Link to comment
Share on other sites

Jacques1 - I  appreciate your comments but I don't understand what your alternate approach entails.  I would love to do this the "right" way and make you satisfied with my code, but I don't know what you mean!  Are you possibly suggesting that I actually output a js array from my php code and just assign that value in my script section of my eventual output or to a file that then gets included in my output?  Probably not, but that's all I see available.

Edited by ginerjm
Link to comment
Share on other sites

The first thing I suggest is that you ditch this terrible inline scripting. No more onclick attributes, no more PHPHTMLCSSJavaScript spaghetti code. Your HTML document should contain HTML only.

 

So all JavaScript, CSS and whatnot goes into external files. If you want to bind an event handler to an HTML element, you do that with pure JavaScript. In jQuery, it's as easy as calling the click() method. In plain JavaScript, you have to fumble with addEventListener() or attachEvent() depending on the browser. 

 

The data must also be fetched with pure JavaScript. Instead of making PHP dump some string into an onclick attribute, you use one of the techniques we've already discussed: Ajax or a div data container.

Link to comment
Share on other sites

Since you haven't seen my script, I'm afraid I don't know what you mean by spaghetti code?  My code is (imho) very well structured and does not mingle html and php and js except for where the logic drives the creation of it.  In this script I use php to gather all the attributes of my files that I need to create the html img tags and pass the variable created to my html output function.

 

Second - I have no event handlers - simply event attributes on html tags.  Do you call that an event handler? 

 

Lastly - I believe I have said that I don't know how I would use an ajax-called script any differently.  As many people (who know far less than I think I do) say - can you tell me what you mean in coding terms?  I am very amenable to doing the right thing - I just don't know what it is.

Link to comment
Share on other sites

Since you haven't seen my script, I'm afraid I don't know what you mean by spaghetti code?  My code is (imho) very well structured and does not mingle html and php and js except for where the logic drives the creation of it.  In this script I use php to gather all the attributes of my files that I need to create the html img tags and pass the variable created to my html output function.

 

You have three different languages nested in each other: A piece of JavaScript code within HTML markup within a PHP script. To me, this is the very definition of spaghetti code.

 

Ideally, you shouldn't have any language nesting at all. The languages should be entirely separated with well-defined interfaces for exchanging data. For example:

  • PHP scripts should only contain PHP code and nothing else (except maybe SQL queries). All HTML markup is kept in external template files. Those templates are managed with a template engine like Twig or Smarty. To pass values from PHP to HTML, you pass them to the rendering component of the engine.
  • HTML documents should only contain HTML and nothing else. All JavaScript and CSS is kept in external files. To pass values from PHP to JavaScript, you use Ajax.
  • JavaScript files should only contain JavaScript and nothing else, especially no HTML markup.

 

 

 

Second - I have no event handlers - simply event attributes on html tags.  Do you call that an event handler? 

 
Yes.
 
 
 

Lastly - I believe I have said that I don't know how I would use an ajax-called script any differently.  As many people (who know far less than I think I do) say - can you tell me what you mean in coding terms?  I am very amenable to doing the right thing - I just don't know what it is.

 

I've already posted several code examples, but appearently that didn't help. Like I said in the other thread, I think you should try this out yourself.

Link to comment
Share on other sites

YOu posted samples of jquery that I've already said I haven't yet learned.  So they are not very much use to me now.  That is if those examples are what you think my JS should be doing.

 

You didn't answer my last question, nor did you explain what a 'javascript string' (your term) is.  Please tell me what you want my php script to return to the ajax caller - in coding terms.

Link to comment
Share on other sites

YOu posted samples of jquery that I've already said I haven't yet learned.  So they are not very much use to me now.

 

C'mon, the code is almost plain English. It doesn't matter that it happens to be jQuery. I just use it as a kind of pseudo code, because writing down 20 lines of low-level JavaScript code only to do an Ajax request is fucking boring and doesn't clarify anything.

 

 

 

You didn't answer my last question, nor did you explain what a 'javascript string' (your term) is.

 

I'm talking about a string within the JavaScript environment:

var some_string = "foo";
var another_string = 'bar';
 

Please tell me what you want my php script to return to the ajax caller - in coding terms.

 

See my reply on your other thread.

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.