I have a form to get information and when i type just spaces with no any other character it gets posted, How could i avoid inserting just spaces in the form?
<form action="index.php" method="post">
<textarea name="textA"></textarea>
<input type="submit" name="sent" value="Send">
</form>
<?php
if(isset($_POST['sent']) && !empty($_POST['textA'])){
$insert=new Insert();
$insert->insertData($_POST['textA']);
}
?>
Just use trim() which will remove all leading and trailing spaces. If there are only spaces in the string then it will become an empty string and empty() will be true.
if(isset($_POST['sent']) && !empty(trim($_POST['textA']))){
You can also do things like str_replace() to replace spaces with and empty string or preg_replace() to do the same but this should do what you need.
Related
When I sanitize the input fields or text area I face a problem. When someone gave spaces and submit the form, my script accepts the form. But I want not to accept fields until there is not written at least a single character. My code is as follows.
Html
<form action="" method="POST">
<textarea name='text'></textarea>
<input type='submit' name='submit'>
</form>
Php
if(isset($_POST['submit'])){
if(isset($_POST['text']) && !empty($_POST['text'])){
//do whatever but not accept white space
}
}
You can trim whatever you want, just by using
trim()
Which removes characters from both sides of a string.
Documentaion: http://php.net/manual/bg/function.trim.php
trim and preg_replace will do this easily
<?php
echo $text = " this is niklesh raut ";
echo "\n";
$text = preg_replace('/\s+/', ' ',$text);
echo trim($text);
?>
live demo : https://eval.in/818137
OUTPUT :
this is niklesh raut
this is niklesh raut
With new line and tab : https://eval.in/818138
You can either echo out your statement:
<?php
if(isset($_POST['submit'])){
if(empty($_POST['text'])){
echo "Please enter a value.";
}
}
Or, add the required attribute to your input field.
<form action="" method="POST">
<textarea name='text' required></textarea>
<input type='submit' name='submit'>
</form>
If i upload file with single quotes that will cause empty name when i do print_r to $_FILE. for example, file named 2'.ogg that system would output .ogg. I think that windows causes this, but i'm not sure. here the code i'm using:
<?php
if(isset($_POST['submit'])) {
print_r($_FILES);
}
echo <<<Print
<form action='' method="POST" enctype="multipart/form-data">
<input type="file" name="t[]"> <input type="submit" name="submit">
</form>
Print;
?>
Use addslashes() to escape single quote and stripcslashes() to remove added slashes.
I have string parameter with apostrophes that I need to pass it to another php page.
My code is:
echo '<form name="submitForm2" action="creatDocument.php?$formulation='.$formulation.'" method="POST">
<input type="submit" value="pass"/>
</form>';
The $fomulation parameter contain the string with hebrew characters that came from user.
if $fomulation = אבג"דה
creatDocument.php received just $fomulation = אבג .
How can I fix it?
What's happening is that the URL parser is breaking on the single quotes. Check out the URLEncode method, to encode your query string parameters.
http://us3.php.net/urlencode
echo '<form name="submitForm2" action="creatDocument.php?$formulation='.urlencode(utf8_encode($formulation)).'" method="POST">
<input type="submit" value="pass"/>
</form>';
Html
<html>
<body>
<h1>What do you want?</h1>
<form action="googleApi.php" method="post" />
<input type="text" name="itemWanted" /> <br />
<input type="submit" name="submit" />
</form>
</body>
</html>
PHP
<?php
//Escape the input
$itemQueried = mysql_real_escape_string($_POST['itemWanted']);
How do I get this code to switch a + sign in for any space for the value $_POST['itemWanted'] so right now, if i enter in 'bag', it works fine. But if I enter in 'Gucci Bag', it doesnt populate the array, because the query (to the script) looks like 'Gucci Bag', when the queries should really be 'Gucci+Bag' if you use a space. How do I switch a + for a <space>?
You can use urlencode, this will convert any spaces to +.
e.g.
urlencode("Gucci Bag"); // Will give "Gucci+Bag"
So just use urlencode($_POST['itemWanted']) instead of $_POST['itemWanted'] and any spaces the user entered will be converted to + for when you create $url.
I have a simple php file with a GET from a form to throw up some files from a directory. Quite simple code but I just need to strip from the string ANY characters that are NOT lowercase alpha. How would I go about doing that? (I'm a novice).
Here 'tis:
<?php $text = $_GET['text_string']; ?>
<form method="GET" action="index.php">Please enter some letters: <input type="text" name="text_string" value=""/> and hit <input type="submit" value="enter" />
</form>
<?php /* Split characters into an array */ $array = str_split($text); ?>
<?php foreach($array as $char) : ?>
<img src="glyphs/<?php print ($char); ?>.jpg"/>
<?php endforeach ; ?>
Thanks!
To lowercase (note that you also can use mb_strtolower for better charset handling, but in this case you're only going to keep ASCII chars anyway so strtolower is enough):
$text = strtolower($text);
To remove all non-alpha chars using preg_replace:
$text = preg_replace('/[^a-z]/', '', $text);