php validation on a empty field - php

hi guys i am kinda new to php and i am trying to add validation on to the form. i want it so the form will not submit if it is empty.
<form id="form1" name="form1" method="post" action="category_created.php">
Enter a New Category Name :
<label for="cat"></label>
<input type="text" name="cat" id="cat" />
<input type="submit" name="submit" id="submit" value="Submit" />
</form>
and the form is being submitted to the file where the contents is being submitted to the database:
<?php
//category name received from 'new_category.php' is stored in cariable $cat
$cat=$_POST['cat'];
$qry=mysql_query("INSERT INTO category(category)VALUES('$cat')", $con);
if(!$qry)
{
die("There Was An Error!". mysql_error());
}
else
{
echo "<br/>";
echo "Topic ".$cat." Added Successfully";
echo "<br/>";
}
?>
any help will be appreciated
thanks

use onsubmit and validate like below
<form onsubmit="return validate()" id="form1" name="form1" method="post" action="category_created.php" >
.....
</form>
and in javasctipt
<script>
function validate(){
if(document.getElementById('cat').value.length<1)
{
alert('Please enter the Category Name');
return false;
}
else
{
return true;
}
}
</script>

or you could submit the form via ajax, in the db-file you check whether the fields are empty, ifso echo an error message and print it via javascript. By doing this you can style everything the way you want to :)

Related

PHP Submit button doesn't have any effect (PhpStorm)

I updated the question.
Since the last code was pretty complex and even after fixing the stuff it didn't work, I executed the below simple code to check if things work. Even this code doesn't work. Whenever I click on the submit button, it again returns a 404 error.
Yes, I placed the PHP code in the body as well to check if this work but it doesn't.
<?php
if(isset($_POST['submit'])) {
echo("Done!!!!");
} else {
?>
<html>
<head>
<title>Echo results!</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input name="submit" type="submit" value="submit"/>
</form>
<?php
}
?>
</body>
</html>
Try giving the button_create as name of the submit button
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
if(isset($_POST['button_create'])) {
<td><input type="submit" name="button_create" id="button_create" value="Create Table!"></td>
change these lines see how you go from there
There are a couple of things wrong here, method should be POST instead of GET. The name attribute of text fields should be used when receiving the values. The submit button name should be used to check whether the button is clicked or not. See the example given below.
<?php
if (isset($_POST['submit'])) {
$ex1 = $_POST['ex1'];
$ex2 = $_POST['ex2'];
echo $ex1 . " " . $ex2;
}
?>
<form action="" method="post">
Ex1 value: <input name="ex1" type="text" />
Ex2 value: <input name="ex2" type="text" />
<input name="submit" type="submit" />
</form>
Echo results!
<?php
if(isset($_POST['submit'])) {
echo("Done!!!!");
} else {
?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input name="submit" type="submit" value="submit"/>
</form>
<?php
}
?>
this is for your updated question

PHP submission not working with either $_POST or $_REQUEST

I have been trying to get the PHP code to submit an email to a mysqlDB, but for some reason it is not working:
This is the form code in the HTML
<form class="header-signup" action="registration.php" method="post">
<input name="email" class="input-side" type="email" placeholder="Sign up now">
<input type="submit" value="Go" class="btn-side">
<p class="hs-disclaimer">No spam, ever. That's a pinky promise.</p>
</form>
For the PHP, I did the following (DB connection infos set to xxxxx):
<?php //start php tag
//include connect.php page for database connection
$hostname="xxxxxx";
$username="xxxxxx";
$password="xxxxxx";
$dbname="xxxxxx";
mysql_connect($hostname,$username, $password) or die ("<html><script language='JavaScript'>alert('Unable to connect to database! Please try again later.'),history.go(-1)</script></html>");
mysql_select_db($dbname);
//Include('connect.php');
//if submit is not blanked i.e. it is clicked.
If(isset($_POST['submit'])!='')
{
If($_POST['email']=='')
{
Echo "please fill the empty field.";
}
Else
{
$sql="INSERT INTO MailingList (MAIL) VALUES('".$_POST['email']."')";
$res=mysql_query($sql);
If($res)
{
Echo "Record successfully inserted";
}
Else
{
Echo "There is some problem in inserting record";
}
}
}
?>
Do you know what might be the problem?
The php file is in the same folder than the webpage.
Thanks for your time
Regards
$_POST['submit']
does not exist, you have to specify the name for the submit button
<input type="submit" name="submit"........>
Please try this
You could also use this conditional for a POST request
if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
And check the input with a var_dump($_POST); to see if the value exists in the array.
This is only if you're expecting one form. If you want multiple forms on the page you could make use of naming your submit button in the HTML code
<form class="header-signup" action="registration.php" method="post">
<input type="submit" name="action1" value="Go" class="btn-side">
</form>
You could also use this if you set an name on the submit button
if(isset($_POST['action1']))
{
var_dump("hit");
}

Check in PHP if form was submitted when used javascript to submit the form

I have this code :
<html>
<head>
<title>Title</title>
</head>
<body>
<form name="selectForm" id="selectForm" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<input type="checkbox" name="checkbox" form="selectForm" />CheckBox
</form>
<input type="button" value="Submit" name="submit" onclick="document.selectForm.submit();" />
<?php
if(isset($_POST['submit'])) {
echo "Submitted";
} else {
echo "Not submitted";
}
?>
</body>
</html>
I can not detect if the form was submitted. I would like to modify the PHP code and not the html, if possible.
Previously I had my Submit type input inside the form and it worked, but now that it is outside and I use JavaScript to do the submit it does not work.
How can I detect if the form is submitted ?
The $_POST['submit'] will no longer exist, since the submit button is no longer part of the form. Instead we can check the $_SERVER['REQUEST_METHOD'].
if($_SERVER['REQUEST_METHOD'] === 'POST') {
echo 'Submitted';
}
Also, if you leave the form's action attribute blank, it will submit to the current page:
<form name="selectForm" id="selectForm" action="" method="post">
Update:
Add a hidden field with the name of your form:
<input type="hidden" name="formname" value="selectForm" />
<?php
if(isset($_POST['formname'])) {
echo $_POST['formname'] . ' submitted';
}
?>

Form Submit Button Works, but not Submit() in Link

I've done this so often before on different websites, but can't get it to work now.
I've got a simple form that posts perfectly well using a submit button, but for a specific reason I actually need it to submit via a url link instead. I'm using submit(). The form submits, but the data isn't posting.
What am I missing?
<html>
<body>
<?
if(isset($_POST['bar'])) { echo 'testing button<br>'; }
if(isset($_POST['information'])) {
echo $_POST['information'];
echo '</br>Info successfully posted.';
}
?>
<form action="test.php" method="post" id="fooform">
Hello World.<br>
Select checkbox: <input type="checkbox" id="information" name="information" value="yes">
<input type="submit" name="bar" value="Send"><br>
Confirm and Post<br>
Post Directly
</form>
<script type="text/javascript">
function SubmitForm(formId) {
var oForm = document.getElementById(formId);
alert("Submitting");
if (oForm) { oForm.submit(); }
else { alert("DEBUG - could not find element " + formId); }
}
</script>
</body>
</html>
The form starts to submit, then the href of the link is followed, and this cancels the form submission.
If you are using old-style onclick attributes, then return false; at the end to prevent the default action.
You would, however, be better off using a submit button (you are submitting a form). You can use CSS to change its appearance.
Try this code :
<html>
<body>
<?php
if (isset($_POST['bar'])) {
echo 'testing button<br>';
}
if (isset($_POST['information'])) {
echo $_POST['information'];
echo '</br>Info successfully posted.';
}
?>
<form action="test.php" method="post" id="fooform">
Hello World.<br>
Select checkbox: <input type="checkbox" id="information" name="information" value="yes">
<input type="submit" name="bar" value="Send"><br>
Confirm and Post<br>
Post Directly
</form>
<script type="text/javascript">
function SubmitForm(formId) {
var oForm = document.getElementById(formId);
alert("Submitting");
if (oForm) {
oForm.submit();
}
else {
alert("DEBUG - could not find element " + formId);
}
}
</script>
</body>
</html>
try to submit form with form id in jquery
<a class="submit">Post Directly </a>
$('a.submit').click(function(){
$('#fooform').submit();
})

submit data via URL bar in PHP and HTML

I have this very basic form in my html page.
<form action="post.php" method="post">
Message: <input type="text" name="message" />
<input type="submit" name="submit" value="send">
</form>
and then stores the data onto my database backend.
id also want to submit data via URL bar, such as this.
http://localhost/test.php?message=test&submit=send
but when i try to do above, nothing happens.
how can i achieve such method?
[EDIT]
my post.php
<?php
include_once("connect.php");
if (isset($_GET['submit'])) {
if ($_GET['message'] == "") {
echo " no input, return";
exit();
}
else {
$message = $_GET['message'];
mysql_query("insert into data (message) values ('$message')");
header ('location:index.php');
exit ();
}
}
else {
echo "invalid";
}
?>
use GET method instead of POST
so your code should be like follow:
<form action="post.php" method="GET">
Message: <input type="text" name="message" />
<input type="submit" name="submit" value="send">
</form>
and in the post.php you can get those Query string by using $_GET['message'] or $_REQUEST['message']
use a form GET method. to submit data of a form as a query string.
<form action="test.php" method="GET">

Categories