Jump to content

[SOLVED] dealing with forms


irkevin

Recommended Posts

Hi, sorry for bugging you again..

 

I have a form.. the php code to insert to data in the database works well..

 

but now, i'm trying to prevent people from being childish with the form.. sorry for the expression..

 

If i do

 

<?php
if(!$_POST['name']){
echo "You did not enter your name";
}
?>

 

basically, it will tell the user that he didn't enter his name.. But i noticed on the net, some form, when you just hit spacebar once and then click submit, it submits the data and obviously, you'll get only a space in the database.. Why is it so? Is there a way to prevent this?

 

If someone know how to prevent this, plz tell me. will be very grateful..

addslashes adds a slash after something strip slashes removes slashes from something.

 

 

if you want to remove whitespace you need to use  the trim function

 

also empty function if nothing is input into that field it wont allow you to post it errors

it still enter a space.. here's the code

 

<?php
session_start();

require('config.php');
require('../functions.php');

$id = "0";
$shout = trim($_POST['shout_msg']);
$shouter = $_SESSION['user_name'];
$timeposted = date("F j, Y, g:i a");



if(!$shouter){
//do nothing
}else{

$conn = mysql_connect("localhost","****","****");
$db = mysql_select_db("kevin",$conn);
$sql = "INSERT INTO myshoutpro_shoutbox VALUES ('$id', '$shouter', '$shout','$timeposted')";
$result = mysql_query($sql);
}
?>
<html>
<meta http-equiv="refresh" content="0;url=http://www.mu-anime.com/shoutbox/index.php" />
</html>

 

whats wrong in this.. im going to smash my forehead on the keyboard :P

yes it is entering in the database.. and it's showing a space on the shoutbox too..

 

basically it shows the space when i hit spacebar and press enter.. but i don't want that to happen even if i hit spacebar and press enter.. Im lost :S

<?php
session_start();

require('config.php');
require('../functions.php');

$id = "0";
$shout = $_POST['shout_msg'];
$shoutt= str_replace(' ', '', $shout);
$shouter = $_SESSION['user_name'];
$timeposted = date("F j, Y, g:i a");



if(!$shouter){
//do nothing
}else{

$conn = mysql_connect("localhost","username","password");
$db = mysql_select_db("$db_name",$conn);
$sql = "INSERT INTO myshoutpro_shoutbox VALUES ('$id', '$shouter', '$shoutt','$timeposted')";
$result = mysql_query($sql);
}
?>
<html>
<meta http-equiv="refresh" content="0;url=http://www.mu-anime.com/shoutbox/index.php" />
</html>

 

here it is

<?php
session_start();

require('config.php');
require('../functions.php');

$id = "0";
$shoutt = trim($_POST['shout_msg']);
$shoutt= str_replace(' ', '', $shoutt);
$shouter = $_SESSION['user_name'];
$timeposted = date("F j, Y, g:i a");



if(!$shouter){
//do nothing
}else{

$conn = mysql_connect("localhost","username","password");
$db = mysql_select_db("$db_name",$conn);
$sql = "INSERT INTO myshoutpro_shoutbox VALUES ('$id', '$shouter', '$shoutt','$timeposted')";
$result = mysql_query($sql);
}
?>
<html>
<meta http-equiv="refresh" content="0;url=http://www.mu-anime.com/shoutbox/index.php" />
</html>

I tried the code you gave me but it didn't work.. then i tried added this part

 

if(!$shouter || !$shout){

//do nothing

}

 

I added !$shout, and it works perfectly.. so normally, you're replacing a space with this '', which means nothing.. it's same as leaving a field blank, right??

 

so it's the same as !$shout i presume..

 

Am i right? huh.. 0_o.. lol

 

but now when i type a normal text, it goes like this

 

thsiisjustatest

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.