Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-18-2012, 10:52 PM   PM User | #1
uparmored
New to the CF scene

 
Join Date: Feb 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
uparmored is an unknown quantity at this point
Line Breaks Deleted When Passing Variables

Hello, I have been having issues when passing form variables(specifically the textarea) to a php script using the code below. My issue is that all line breaks and extra white space are deleted. It seems my trouble stems from the fact that I'm using GET in my javascript versus POST.

I was wondering if someone had a smart way to convert my javascript to use POST, or if there is another method to do this?

Thanks for any and all insight in advance!


Javascript:
Code:
<script language="javascript" type="text/javascript">

//Browser Support Code
function submitReview(watchid){
 var ajaxRequest;  // The variable that makes Ajax possible!
 	
 try{
   // Opera 8.0+, Firefox, Safari
   ajaxRequest = new XMLHttpRequest();
 }catch (e){
   // Internet Explorer Browsers
   try{
      ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
   }catch (e) {
      try{
         ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
      }catch (e){
         // Something went wrong
         alert("Your browser broke!");
         return false;
      }
   }
 }
 // Create a function that will receive data 
 // sent from the server and will update
 // div section in the same page.
 ajaxRequest.onreadystatechange = function(){
   if(ajaxRequest.readyState == 4){
      var newtext = "Your review has been submitted to the moderator for approval.";
      document.getElementById('reviewform').innerHTML = newtext;
     }
 }
 // Now get the value from user and pass it to
 // server script.
 var id = document.getElementById('id').value;
 var brand = document.getElementById('brand').value;
 var ovr = document.getElementById('ovr').value;
 var bld = document.getElementById('bld').value;
 var erg = document.getElementById('erg').value;
 var val = document.getElementById('val').value;
 var com = document.getElementById('com').value.replace(/\n/g,'<br />');
 var len = document.getElementById('len').value;
 var use = document.getElementById('use').value;
 var un = document.getElementById('un').value;
 var mdy = document.getElementById('mdy').value;
 var ytURL = document.getElementById('ytURL').value;
 var price = document.getElementById('price').value; 


 var queryString = "?id=" + id ;
 queryString +=  "&brand=" + brand + "&ovr=" + ovr + "&bld=" + bld + "&erg=" + erg + "&val=" + val + "&com=" + com + "&len=" + len + "&use=" + use + "&un=" + un + "&mdy=" + mdy + "&price=" + price + "&ytURL=" + ytURL;
 ajaxRequest.open("GET", "sendreview.php" + 
                              queryString, true);
 ajaxRequest.send(null); 



}

</script>

*EDIT* I figured this out. I added the code in red and then converted it back with a preg_replace in the PHP.

Last edited by uparmored; 02-19-2012 at 01:13 AM..
uparmored is offline   Reply With Quote
Old 02-22-2012, 01:48 PM   PM User | #2
Rowsdower!
Senior Coder

 
Rowsdower!'s Avatar
 
Join Date: Oct 2008
Location: Some say it's everything.
Posts: 2,007
Thanks: 5
Thanked 395 Times in 388 Posts
Rowsdower! has a spectacular aura aboutRowsdower! has a spectacular aura aboutRowsdower! has a spectacular aura about
I see that you have already sorted out the line break issue, but to answer your question regarding the use of POST rather than GET for your AJAX, take a closer look at this part:

Code:
 ajaxRequest.open("GET", "sendreview.php" + 
                              queryString, true);
 ajaxRequest.send(null);
You might be able to guess what needs to be changed there to get rolling with a POST request.
__________________
The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
See Mediocrity in its Infancy
It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
Seek and you shall find... basically:
validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting
Rowsdower! is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:51 PM.


Advertisement
Log in to turn off these ads.