开发者

Whenever I use ajax to get data from my textarea and insert it into mysql, line breaks are not inserted

I am using ajax to insert a form using GET. When that form is submitted, it goes into a mysql database. I know that the error is occurring when this data is being submitted into mysql and not when I am retrieving it. My problem is that all line breaks, and when you press the "enter" key are not being submitted into the database. All of the text just goes in as a straight line without breaks or anything of the sort. I would appreciate any help as to figuring out how to get these breaks to actually be inserted into mysql because this is a big problem for my site. Any help is very much appreciated.

Here is the code for the ajax that I am using

$echovar400=
      "
    <script language='javascript' type='text/javascript'>

function ajaxFunction(){
    var ajaxRequest;  

try{
    // Opera 8.0+, Firefox, Saf开发者_C百科ari
    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;
        }
    }
}
ajaxRequest.onreadystatechange = function(){
    if(ajaxRequest.readyState == 4){
        var ajaxDisplay = document.getElementById('pagecomments');
        ajaxDisplay.innerHTML = ajaxRequest.responseText;

    }
}
var age = document.getElementById('age').value;
var wpm = document.getElementById('wpm').value;
var queryString = '?age=' + age + '&wpm=' + wpm;
ajaxRequest.open('GET', 'ajaxprofilechat.php' + queryString, true);
ajaxRequest.send(null); 

}

</script>

<form name='myForm' method='GET' >
<textarea rows='4' name='message' class='comment' maxlength='250' id='age' wrap='hard'>        </textarea><br><h40>
<input type='hidden' id='wpm' value='$profilename'/>
<input type='button' onclick='ajaxFunction()' value='Comment' />
</form>
    ";
  }
  ?>

I realize that is not the start of the php, but the rest is unimportant.

here is the code for ajaxprofilechat

$age = strip_tags($_GET['age']);
$wpm = $_GET['wpm'];
// Escape User Input to help prevent SQL Injection
$wpm = mysql_real_escape_string($wpm);
$chatname6 = ($_SESSION['username']);
$message6 = $_GET['site_message'];
$month6 = date("F");
$dayofmonth6 = date("d");
$year6 = date("Y");
$date10 = "$month6 $dayofmonth6 $year6";

$hours6 = date("g");
$min6 = date("i");
$sec6 = date("s");
$amorpm6 = date("A");
$time6 = "$hours6:$min6 $amorpm6";

if (strlen($age)>4)
{
mysql_connect("","","") or die($error);
mysql_select_db("") or die($error);
mysql_query("INSERT INTO guestbook VALUES             ('','$wpm','$chatname6','$age','$date10','$time6')");
 echo "&nbsp;<h80><b>Comment Posted</b></h80<p><p>";
 }
 else
 {
 echo "&nbsp;<h80><b>Your comment must be greater than four characters</b></h80><p>";
 }
 ?>

Any help would be great. Thanks!

if you need to see my site to look at the error, here is a link to my profile page http://www.pearlsquirrel.com/profile.php?u=eggo


Guys I have literally tried everything that you have told me, and in every possible way. However, i am still encountering the same problem. Should I try to use the POST method of ajax instead of GET? Do you have any other suggestions? And thank you for the help so far.


Try $message6 = nl2br($_GET['site_message']);, then you don't have to worry about the \n or \r\n in your MySQL record since it will be stored as HTML <br /> and will display as intended during output. If you need to put it back in a textfield for edit, you'd simply use br2nl().

function br2nl($str)
{
    return preg_replace('#<br\s*?/?>#i', "\n", $str); 
}


try to wrap the POST data from the textarea with the nl2br() function

nl2br

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜