4

Please pardon my lack of 100% specific technical language… I'm a bit of a hack...

A backend developer (who's working on a server I don't have access to) has set up a form action for me, and I'm wondering what's the best way to use that form action's response as error or success messages for the user. For example, when I hit the form action with a GET variable (such as remote-server.com/action.cfm?email=nothing) the remote cfm page will display only "-3", which is the error code for "bad email". There are a series of error/success codes the developer has set up corresponding with other error/success cases.

So I'm looking for a solution that basically does:

  1. Takes the form data and passes it to remote-server.com/action.cfm without directing the user to that page.

  2. Reads the response from remote-server.com/action.cfm, which will be a comma-delineated series of numbers.

  3. Assign those numbers as a string in PHP, so I can sniff out the error/success codes and display the appropriate messages in my front-end code.

So again, the problem in a nutshell is that I want to use form action="remote-server.cfm"... but WITHOUT the page redirecting the user to that cfm page. And instead, it should get the cfm page's output into my form's PHP page so I can process it. Does this make sense? I'm looking around and people seem to say AJAX is the only way to do this. Any other thoughts? Thanks so much!

user1399181
  • 303
  • 4
  • 10
  • 1
    Sounds like you want to POST the form via ajax, and read back the response. [jQuery.post](http://api.jquery.com/jQuery.post/) can help you with that. You can then map the error codes using a JS object `{-3:'bad email,-2:'bad developer'}` and display the appropriate message. – mpen May 16 '12 at 16:55

2 Answers2

4

That is correct, seems like you need to use Javascript and AJAX. With an AJAX call you send an asynchronous post query to that server in the background without redirecting the user to any other pages, and parse the response variables.

If the submit has errors, you can show proper errors (or even add form validations if you know what the server expects and what it doesn't even before submitting it) and if the AJAX call succeeds, just redirect the user to a different page (and don't post the form anymore, since it has already been submitted once)

Reza S
  • 9,480
  • 3
  • 54
  • 84
  • This makes sense. I'm going to look into jQuery.post - seems that should be able to do everything I need. Thanks so much everyone - you are an amazing community! – user1399181 May 16 '12 at 17:08
  • Looks like I hit a snag - since the remote server is not on the same domain as my php file, all the ajax requests are coming back blank. Is something like this my best solution? http://www.ajax-cross-domain.com/ – user1399181 May 16 '12 at 18:12
  • Have a look at this: http://stackoverflow.com/questions/3506208/jquery-ajax-cross-domain – Reza S May 17 '12 at 01:39
0

Why not use AJAX? Have JavaScript/jQuery to attach a submit handler on the form, and submit it sliently with AJAX, then read the appropriate response and whatnot, and display it nicely for the user.

If JavaScript is disabled, the form will be submitted normally, which is not pretty, but should still be usable.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308