Before i get to the code i posted below ill highlight what i found…
When using input fields its best to wrap them inside a <form> element for semantic markup and so it can be used without the need for javascript
A quick tip a co-worker taught me was to use event.[COLOR=SeaGreen]preventDefault/COLOR as rather then returnfalse it doesn’t allow for the script to continue or reload the page if an error occurs.
In your texter.php file you had [B]isset/B which wouldn’t have worked at all as your AJAX call didn’t have any data parameters that contained that key name.
When writing your code its best not to use classes too much as they take longer to find on a page due to them been allowed to appear multiple times. Id’s are a more efficient and less time consuming way to write your markup.
form.php
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(function() {
$('#myForm').submit(function(e) {
// Prevent the default form action
e.preventDefault();
// Get the value of the "textValue" field
var textValue = $('input[type="text"]', this).val();
// Make sure that the "textValue" field has a value/length
if (!textValue.lenth || textValue == '') {
$('#success').fadeOut(200);
$('#error').fadeIn(200);
return false;
}
$.ajax({
data : {text: textValue},
type : $(this).attr('method').toUpperCase(),
url : $(this).attr('action'),
success : function(data) {
if (data === 'success') {
$('#error').fadeOut(200);
$('#success').fadeIn(200);
} else {
$('#success').fadeOut(200);
$('#error').fadeIn(200);
}
}
});
});
});
</script>
<form action="texter.php" method="post" id="myForm">
<input type="text" name="textValue" />
<input type="submit" value="Submit" />
</form>
<span id="error" style="display: none;">ERROR FIELD IS EMPTY</span>
<span id="success" style="display: none;">DONE</span>
hmm thanks, but i don’t know where is problem but…
Firebug -> Net ^ All ^ says
POST form.php -> mypage.com/texter.php ^ Post Parameters - application/x-www-form-urlencoded text asdasd Source
text=asdasd
js i think works fine because he is giving post but always it says ERROR FIELD IS EMPTY, if i submit text or nothing submit, but i think this php code doesn’t work…
if (isset($_POST['text'])) {
$text = quote_smart3(htmlspecialchars($_POST['text']));
if (empty($text)) {
echo 'error';
} else {
The data variable is whatever the php file outputs as text. The JavaScript code expects it to output ‘success’ so you need to check what texter.php actually outputs.
thanks, fixed!
do u have any idea how to give for js this Message added?
die('success');
just now i have content before the die and now he is saying again Sorry the field is empty!
and refreshing chat without page reload after submiting? because when i hit on enter then i can floood many many messages…
The response from chat.php is much more than just “success”. This is what the Ajax request is receiving from chat.php
<div id="chat_container">
<div id="chat_content">
<script type="text/javascript">
$(function() {
$('#myForm').submit(function(e) {
// Prevent the default form action
e.preventDefault();
// Get the value of the "textValue" field
var textValue = $('input[type="text"]', this).val();
// Make sure that the "textValue" field has a value/length
if (!textValue.length || textValue == '') {
$('#success').fadeOut(200);
$('#error').fadeIn(200);
return false;
}
$.ajax({
data : {text_post: textValue},
type : $(this).attr('method'),
url : $(this).attr('action'),
success : function(data) {
if (data === 'success') {
$('#error').fadeOut(200);
$('#success').fadeIn(200);
} else {
$('#success').fadeOut(200);
$('#error').fadeIn(200);
}
}
});
});
});
</script>
<div id="ajax_chatinc"><div class="loading"></div></div>
success
Notice the “success” at the end? Even though it’s at the end, it’s the whole output from chat.php that is assigned to the data variable.
So if you want “success” to work, you need to stop the other code from being outputted.
fixed! thaaanks!
so… do u have any idea to refresh chat without page reload after submiting in my situation? because when i hit on enter then i can floood many many messages…
I understand your words individually but collectively they make very little sense. That might be due to it being near 2:30am though, so let’s hope someone else can make better sense of them than I.