Log in

View Full Version : Ajax don't send ENTER's to PHP script



auriaks
04-03-2010, 04:48 PM
Hi,
I have AJAX form, which sends text to message.php page ans there text is inserted to the mysql.

Here is my message.php


<?php
if (isset($_GET['action'])) {
$nikas = $_GET['nick'];
$zinute = $_GET['zinute'];
$failas = $_GET['failas'];
$time = date("H:i:s");

include $_SERVER['DOCUMENT_ROOT'] . '/content/processes/db_conn.php'; //db

$nikas = mysql_real_escape_string($nikas);
$zinute = mysql_real_escape_string($zinute);
$failas = mysql_real_escape_string($failas);
$nikas = mysql_real_escape_string($nikas);

$zinute = str_replace('\r\n','<br \>',$zinute);

if($zinute == '') {
$error .= "Prašome įvesti tekstą.";
}

if(isset($error)) {
echo "$error";
} else {
mysql_query("INSERT INTO `pamoka_zinutes` (Nick, Time, Zinute, failas)
VALUES('$nikas', '$time', '$zinute', '$failas')") or die(mysql_error());
}}

if (isset($_GET['truncate'])) {
include $_SERVER['DOCUMENT_ROOT'] . '/content/processes/db_conn.php'; //db
$nikas = $_GET['truncate'];
$nikas = mysql_real_escape_string($nikas);
if ($nikas == 'admin') {
mysql_query("TRUNCATE TABLE `pamoka_zinutes`");
echo "Žinutės ištrintos";} else {echo "Tik mokytojas gali trinti žinutes";}
}
?>


This part should detect ENTER pushing but it doesn't

$zinute = str_replace('\r\n','<br \>',$zinute);

What is wrong?

Thanks for your time.

bluewalrus
04-03-2010, 09:43 PM
\n and \r are read literally as the text reads not as "new line" and "return", try using double quotes ".

You may also need to separate them.


$zinute = str_replace("\r", '<br \>', $zinute);
$zinute = str_replace("\n", '<br \>', $zinute);

djr33
04-03-2010, 10:16 PM
nl2br() will do that.

auriaks
04-04-2010, 07:04 PM
I am using
httpObject3.open("GET", "post.php?action="
+document.getElementById('send').value+"&zinute="
+document.getElementById('zinute').value+"&failas="
+document.getElementById('failas').value+"&nick=<?php echo "$nick";?>", true);

in my AJAX script which sends the information to post.php (message.php) page...
Maybe some java will do <br /> effect? I mean doing like this
$zinute = str_replace("\n", '<br \>', $zinute);