...

View Full Version : how do i do so it doesnt echo a blank input ?



Nightcrawler89
08-19-2011, 11:50 PM
I have a form, and it a javascript calls a php that echo the input on an "ul" using ajax.

the thing is that, it echos everything, even when the form is blank, with no text, it echos that "blank" value.

How do i do so it doesnt echo when the input is blank?



<?php
if(isset($_POST['addcontentbox'])){
/* Connection to Database */
include('config.php');
/* Prevent Query Injection */
$message = mysql_real_escape_string($_POST['addcontentbox']);
/*echo*/
$sql = 'INSERT INTO WALL (message) VALUES( "'.$message.'")';
mysql_query($sql);
echo $message;
} else { echo '0'; }
?>


Note:
addcontentbox = the input id/name.
config.php is the databse config
"wall" is the id of the unsorted list.


Any sugestions? guide? =[

BluePanther
08-20-2011, 12:48 AM
I have a form, and it a javascript calls a php that echo the input on an "ul" using ajax.

the thing is that, it echos everything, even when the form is blank, with no text, it echos that "blank" value.

How do i do so it doesnt echo when the input is blank?



<?php
if(isset($_POST['addcontentbox'])){
/* Connection to Database */
include('config.php');
/* Prevent Query Injection */
$message = mysql_real_escape_string($_POST['addcontentbox']);
/*echo*/
$sql = 'INSERT INTO WALL (message) VALUES( "'.$message.'")';
mysql_query($sql);
echo $message;
} else { echo '0'; }
?>


Note:
addcontentbox = the input id/name.
config.php is the databse config
"wall" is the id of the unsorted list.


Any sugestions? guide? =[

Wrap your code in PHP tags, not CODE tags :)

But, to your main question, you need to check if $_POST['addcontentbox'] is empty. Do this: (I formatted your code better as well, to make it more readable :) )


<?php
if( isset($_POST['addcontentbox']) && !empty($_POST['addcontentbox']) ){

// Connection to Database
include('config.php');

// Prevent Query Injection
$message = mysql_real_escape_string($_POST['addcontentbox']);

// echo
$sql = 'INSERT INTO WALL (message) VALUES( "'.$message.'")';
mysql_query($sql);

echo $message;
}
else {
echo 'You must fill out the content box!';
}
?>

Should work :)

Nightcrawler89
08-20-2011, 02:49 AM
Wrap your code in PHP tags, not CODE tags :)

But, to your main question, you need to check if $_POST['addcontentbox'] is empty. Do this: (I formatted your code better as well, to make it more readable :) )


<?php
if( isset($_POST['addcontentbox']) && !empty($_POST['addcontentbox']) ){

// Connection to Database
include('config.php');

// Prevent Query Injection
$message = mysql_real_escape_string($_POST['addcontentbox']);

// echo
$sql = 'INSERT INTO WALL (message) VALUES( "'.$message.'")';
mysql_query($sql);

echo $message;
}
else {
echo 'You must fill out the content box!';
}
?>

Should work :)

Thanks !!! , looks much more readable now, but, its not working =( ...

maybe i have to do it thru javascript.

BluePanther
08-20-2011, 03:31 AM
Thanks !!! , looks much more readable now, but, its not working =( ...

maybe i have to do it thru javascript.

Any more information than 'it's not working'? :)

Only touch javascript if you want animation. What exactly is the result you're looking for?

tangoforce
08-20-2011, 03:35 AM
(I formatted your code better as well, to make it more readable :) )

No, no, no... thats not readable :p

This is much better :D


<?php
if( isset($_POST['addcontentbox']) && !empty($_POST['addcontentbox']) )
{
// Connection to Database
include('config.php');

// Prevent Query Injection
$message = mysql_real_escape_string($_POST['addcontentbox']);

// echo
$sql = 'INSERT INTO WALL (message) VALUES( "'.$message.'")';
mysql_query($sql);

echo $message;
}
else
{
echo 'You must fill out the content box!';
}
?>



Much easier to see the opening and closing brace relations :cool: Plus it works better in Notepad++ :thumbsup:

(I never understood the { at the end of one line and the } at the beginning of another - along with other closing braces - far too confusing!)

BluePanther
08-20-2011, 05:18 AM
No, no, no... thats not readable :p

This is much better :D


<?php
if( isset($_POST['addcontentbox']) && !empty($_POST['addcontentbox']) )
{
// Connection to Database
include('config.php');

// Prevent Query Injection
$message = mysql_real_escape_string($_POST['addcontentbox']);

// echo
$sql = 'INSERT INTO WALL (message) VALUES( "'.$message.'")';
mysql_query($sql);

echo $message;
}
else
{
echo 'You must fill out the content box!';
}
?>



Much easier to see the opening and closing brace relations :cool: Plus it works better in Notepad++ :thumbsup:

(I never understood the { at the end of one line and the } at the beginning of another - along with other closing braces - far too confusing!)

PREFERENCE :P.

I don't use notepad++ anyway. I'm a netbeans kinda guy.

I lay it out like that because of my python background:



if(whatever):
do something


Incase you're not familiar with python syntax :)

Nightcrawler89
08-20-2011, 08:11 AM
Any more information than 'it's not working'? :)

Only touch javascript if you want animation. What exactly is the result you're looking for?

lol, yes sorry. I mean, its still the same, everytime the text input is submited it echos, but is doign echo when is blank also, so it is posting blank spaces when the input is submited without any text on it. =\.


Idk if this would help, but this is the javascript that calls the submit action:


<script type="text/javascript">
$(document).ready(function(){
$("form#postbar_add_post").submit(function() {

var addcontentbox = $('#addcontentbox').attr('value');

$.ajax({
type: "POST",
url: "postear.php",
data:"addcontentbox="+ addcontentbox,
success: function(){
$("ul#wall").prepend("<li>"+addcontentbox+"</li>");
$("ul#wall li:first").fadeIn();
document.postbar_add_post.addcontentbox.value='';
}
});
return false;
});
});
</script>

postbar_add_post = form name.
addcontentbox = text input id.
ul#wall = the ul where the text is posted to.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum