Reverse words in sentence as Ajax request
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Reverse words in sentence as Ajax request

Hybrid View

  1. #1
    Join Date
    Feb 2007
    Posts
    2

    Reverse words in sentence as Ajax request

    I would like to create script where user enters two sentences. Sentence is terminated by dot (.) character. Provide a submit button "Reverse words in sentence". On submit button click, send the input sentence as Ajax request using dynamically created iframe. The backend should reverse the words in each sentence and send back the result to be displayed.

    For Example, if the two sentences are:
    "I am a good student. I will get good job."

    The displayed results should look like
    "student good a am I. job good get will I."

  2. #2
    Join Date
    Jun 2011
    Posts
    35

    Re: Reverse words in sentence as Ajax request

    What kind of script are you using server-side?

    For PHP you have a neat little function called explode() which will create an array based on a string and a separator.
    So given your example:

    Code:
    $sentences_array = explode('.','I am a good student. I will get good job.');
    foreach ($sentences_array as $sentence){
         $words_array = explode(' ', $sentence); //create array from string (separator ' ')
         $words_array = array_reverse($words_array); //invert order of items
         $sentence = implode(' ',$words_array); //glue array together with ' '
    }
    I've not tested this but it should show you the right direction..

    Laurent
    Last edited by blaurent; August 28th, 2011 at 04:41 PM.

  3. #3
    Join Date
    Feb 2007
    Posts
    2

    Re: Reverse words in sentence as Ajax request

    Thank you sir. I'm using PHP script for server side.
    but how to create iframe dynamically using Ajax request.

  4. #4
    Join Date
    Jun 2011
    Posts
    35

    Re: Reverse words in sentence as Ajax request

    I'm not sure why you need an iframe?

    I assume all you want it to do is reverse the words of the sentences right?

    So why not just go with a html form

    Code:
    <html>
    <body>
    <form id="reverse_words_form" action="post" method="">
    <textarea id="input_sentences"></textarea>
    <label for="input_sentences">Please enter some sentences..</label>
    <input type="submit" value="Submit" />
    </form>
    </body>
    
    </html>
    Then I would use jQuery to add a click-handler to the submit button which would sent the form data using post (as an ajax request) to your back-end php script.
    If you've never done AJAX before you should probably read up on it.

    I used this not too long ago so here's the function reference. There are some examples on this page so don't be afraid of playing around with it. In your case I don't think you'd need to use JSON or XML as a return. I guess a simple string return would work.

    http://api.jquery.com/jQuery.post/

    Using this you would not need iframes, and personally, I would not recommend using those.

    Laurent

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center