Jump to content

[SOLVED] How do I handle carriage returns from a textarea


nafetski

Recommended Posts

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>

Link to comment
Share on other sites

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!

 

Link to comment
Share on other sites

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!

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.