tags:

views:

61

answers:

3

Hi, I have a form that lets a user perform a search for a stock symbol. The data is transmitted to another URL via PHP cURL. This is fine, we do this several other places on the site. I need to be able to trim the returned data (it returns a whole page) and only display the data between the {STARTKINETICK} {ENDKINETICK} parts.

I need to have it displayed in a div#results. Please help! this should be so simple!

$(document).click(function() { 
$("#SymbolSearchForm").submit(function() {
    $.ajax({
     url: "PHP/Kinetick_Symbol_Search.php",
     type: "post",
     dataType: "HTML",
      success: function(html){
        html = $.trim(html);
        html = html.match(/\{STARTKINETICK\}(.*)\{ENDKINETICK\}/)[1];
        var end = "{ENDKINETICK}";
        $("#return").append(html.substring(html.indexOf("{STARTKINETICK}"), html.indexOf(end) + end.length));
    }

    });
});

});

+1  A: 

You can use html.indexOf("{STARTKINETICK}") and html.indexOf("{ENDKINETICK}") to get the starting and ending points and then use html.substring. Don't forget to add the length of "{STARTKINETICK}" to the start point.

EDIT

var start = "{STARTKINETICK}";
html = html.substring(html.indexOf(start) + start.length, html.indexOf("{ENDKINETICK}"); 
RememberME
example please...not sure I follow. thx
Dirty Bird Design
see edit.......
RememberME
Do you see anything wrong with edited code above? it doesn't seem to be binding to my form, it has no ajax functionality
Dirty Bird Design
I've edited my answer. I originally misread the question and thought you wanted the start and end labels too, not just the data inbetween. You should either use my solution or Topera's, but not both. If the substring issue is resolved, you'll probably get the most help by accepting an answer here and creating a new question with your AJAX/append issue. Is your issue that it doesn't return the html, or that it doesn't append to the div?
RememberME
If you doing a submit, but don't want to go to the submit action, you need to `return false;`
RememberME
A: 

Regex that you need:

"foo{STARTKINETICK}myText{ENDKINETICK}bar".match(/\{STARTKINETICK\}(.*)\{ENDKINETICK\}/)[1]; // returns myText

Whole code:

success: function(html){
    html = $.trim(html);
    html = html.match(/\{STARTKINETICK\}(.*)\{ENDKINETICK\}/)[1];
    $("#return").append(html);
}
Topera
could you take a look at my code, its not staying on the page, rather going to the action
Dirty Bird Design
I don't get it...I give you the answer, you put it in your code and I don't give any rep??
Topera
it didn't work, i apologize for not getting you "points" or whatever, if you like Ill make you a badge
Dirty Bird Design
@dirty "Make you a badge" - Ok, I'll never waste my time again trying to help disrespectfull people. Good Luck.
Topera
A: 

A bit off topic, but do you know that jQuery has a nice method - load() that loads html asynchronously. Moreover, you can pass element selector that should be extracted from the result. If you can control the server side, you can wrap {STARTKINETICK} and {ENDKINETICK} in <div id="to_be_extracted"></div> and call

$('#return').load('PHP/Kinetick_Symbol_Search.php #to_be_extracted');

I use this technique here and there and find it very useful.

dmitko
that's pretty slick. so you can submit a form through the PHP/cURL without any of the ajax submit stuff? the only issue is that the returned data doesn't have an ID.
Dirty Bird Design
actually - it's about AJAX, just another 'smart' method.
dmitko