Jump to content

Big problem


paco_santigo

Recommended Posts

I have a problem and it involves #s....I have a form that is partially filled in with data from a mysql query through php.The user then completes the rest of the form, and submits it.When you click the button to submit, it runs a javascript function to go to a new page. This is done using AJAx...Anyways, everything works great, except sometimes The data I pull contains a "#" sign...When the button is clicked activating the javascript, the # is sent to the script, and I think thats whats causing the error... Below is the code for the javascript.

function completeForm(poid,name,cno,job,phone,cpo,date,sec,com){ xmlHttp=GetXmlHttpObject()if (xmlHttp==null) { alert ("Browser does not support HTTP Request") return }var url="completeForm.php"url=url+"?PONo="+poidurl=url+"&name="+nameurl=url+"&cNo="+cnourl=url+"&jobNo="+joburl=url+"&phoneNo="+phoneurl=url+"&custPO="+cpourl=url+"&today="+dateurl=url+"&section="+securl=url+"&comments="+comurl=url+"&sid="+Math.random()xmlHttp.onreadystatechange=stateChanged xmlHttp.open("GET",url,true)xmlHttp.send(null)//window.print()}

For example, the value for jobNo sent to the javascript might be "Marshall Miller #34 Bathroom", but all that is transfered is "Marshall Miller ".... and every variable after is blank aswell. This makes me think it is the # sign.So my question is, how could I make sure the # sign isn't sent to the script, which would make it part of the URL?Better yet, The current method of action is GET (as seen in the above used with AJAX)... I tried replacing it with POST, and making the neccessary changes, but it didnt work... Is this possible... Ajax and POST? All the examples at W3School show only the get method.Thanks, I appreciate your helpChris

Link to comment
Share on other sites

You need to escape the data going into the URL. The # character has a special meaning when it's in a URL, it's not seen as part of the data. The same thing would happen with a ?, &, or = character. You need to escape everything.url=url+"?PONo="+escape(poid)url=url+"&name="+escape(name)etc

Link to comment
Share on other sites

OMG, I love you... lolThanks. Works now.Another question I might run into later. As above, the code shows that I pass a "comments" variable into the URL as well, and i know there's a limit on the length of the URL. So my question is, that if the comments section is too long, and this is passed to the URL... then the url wont contain the full comments entered.I assume this only applies in the get method.,So can AJAX be used with POST?or is there another way around this?

Link to comment
Share on other sites

You can use AJAX with post, do a Google search for those and you'll find examples. I realize that there's a limit in the spec on how long a URL can be, but I don't think any browser or server actually enforces that limit. The limits that are actually used are probably a lot larger, like 2^16 characters or something. But yeah, you can use post also.

Link to comment
Share on other sites

I only use GET queries when I'm literally writing a URL into a link.Mostly I use POST in forms and AJAX, because often I have a script in the same file as an HTML doc, and if the server method is GET then I just skip the script and go right to the HTML. It's the easiest test I know.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...