nafetski Posted December 12, 2007 Share Posted December 12, 2007 Hello everyone! This might be a really easy question, or complex...I've tried googling and searching quite a bit, but I might just be putting in the wrong search criteria. The situation. I'm using Ajax to send javascript variables via a GET method...PHP is retrieving the variables, and stuffing the content I want into a <div> The application is for posting new jobs, and I wanted to have something that would update what the final content would look like in real time. Everything is working fine, except for the part of my form where I use a textarea. When I set the variable in PHP and echo it, I cannot get carriage returns to show up or work at all. What I want to know - is how can I pass some sort of data from a carriage return in a text area (doesn't matter what really) so that I can use a str_replace and just put in a break tag. Here is some code to give you an idea. $jobname = $_GET['jobname']; $salary = $_GET['salary']; $education = $_GET['education']; $jobdescription = $_GET['jobdescription']; $formattedjob = str_replace("[newline]", "<br>", $jobdescription); var_dump($jobdescription); echo "<b>"; echo $jobname; echo "</b>"; echo "<br>"; echo "<br>"; if($salary!="Select A Salary"){ echo $salary; echo "<br>"; echo "<br>"; } if($education!="Select Education Level"){ echo $education; echo "<br>"; echo "<br>"; } echo $formattedjob; ?> Thats the PHP code that is getting pushed into the Div. Here is the javascript code (just in case) that is sending the variables. var jobname = document.getElementById('jobname').value; var salary = document.getElementById('salary').value; var education = document.getElementById('education').value; var jobdescription = document.getElementById('jobdescription').value; var queryString = "?jobname=" + jobname + "&salary=" + salary + "&education=" + education + "&jobdescription=" + jobdescription; ajaxRequest.open("GET", "rightdiv.php" + queryString, true); Once again, the code is working fine...I just can't get carriage returns to show any value from the textarea >.< Edit:. Also adding the code for the textarea. <textarea class='notboring' id='jobdescription' cols='50' rows='8' wrap='hard' ></textarea> Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 12, 2007 Share Posted December 12, 2007 newlines don't have any meaning in HTML (they just format the source code.) HTML needs <br /> to format content with newlines. Take a look at this - http://php.net/nl2br Quote Link to comment Share on other sites More sharing options...
sureshp Posted December 12, 2007 Share Posted December 12, 2007 If you want to use AJAX to handle the form submission and display the result, just try to use prototype.js or jquery.js. It will make your life easier to do AJAX operations. Quote Link to comment Share on other sites More sharing options...
nafetski Posted December 13, 2007 Author Share Posted December 13, 2007 Thanks for the replies =) I'll take a look at the javascript libraries, I'm still very new to JS and Ajax - so I'm sure they'll come in helpful. http://php.net/nl2br didn't help me too much, since the newlines aren't being sent from textarea. Useful function to have tho, I'll keep it handy! Any other suggestions? Going to start looking at these JS libraries and see if they help! Quote Link to comment Share on other sites More sharing options...
nafetski Posted December 13, 2007 Author Share Posted December 13, 2007 Well the solution ended up not being too PHP related - but I found a fix =) Javascript code that loops through the textbox variable for all instances of \n...then replaces it with |n| var jobdescription = document.getElementById("jobdescription").value; var intIndexOfMatch = jobdescription.indexOf("\n"); while (intIndexOfMatch != -1){ jobdescription = jobdescription.replace(/\n/,"|n|"); intIndexOfMatch = jobdescription.indexOf("\n"); } Then the PHP code that replaces |n| to <br> $jobdescription = $_GET['jobdescription']; $formattedjob = str_replace('|n|','<br>' ,$jobdescription); There are probably easier ways to do it, but it's working great for me. Hopefully this helps someone else down the road! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.