1,105,391 Community Members

Can't make any field in form required...

Member Avatar
Verean
Newbie Poster
3 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I've tried everything that I know of, to make the fields required.
The form is quite simple and small.

Here is the visual form code:

<form action="insert.php" method="post">
<table cellpadding="2" border="0">
  <tr>
    <td>Server Name:</td>
    <td>IP:</td>
  </tr>
  <tr>
    <td>
      <input name="Name" type="text" />
   </td>
   <td>
      <input name="IP" type="text" />
   </td>
  </tr>
  <tr>
  <td colspan="2"><?php
require_once('recaptchalib.php');
$publickey = "my public key"; 
echo recaptcha_get_html($publickey);
?>
  </td>
  </tr>
  <tr>
   <td colspan="2">
	    <input type="submit" />
   </td>
  </tr>
</table>
</form>

Here is insert.php:

<?php
$con = mysql_connect("host","username","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("tablename", $con);

$sql="INSERT INTO servers (Name, IP)
VALUES
('$_POST[Name]','$_POST[IP]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added.";

mysql_close($con)
?>

All the database details have been replaced.

Member Avatar
pritaeas
mod_pritaeas
11,316 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,835 [?]
Skill Endorsements: 156 [?]
Moderator
Featured
Sponsor
 
0
 

What do you mean by required? You can check your input fields with Javascript and/or PHP and act accordingly.

Member Avatar
Verean
Newbie Poster
3 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Make them a mandatory field. Im not sure how to go about doing that.

Member Avatar
weekendrockstar
Junior Poster in Training
71 posts since Dec 2010
Reputation Points: 12 [?]
Q&As Helped to Solve: 16 [?]
Skill Endorsements: 0 [?]
 
0
 

Insert.php

<?php
$Name = $_POST['NAME'];
$IP = $_POST['IP'];

if (!isset($Name)) {
echo "You must enter a name.";
exit;
}
elseif (!isset($IP)) {
echo "You must enter an IP address.";
exit;
}
else {
 // code for database insertion and printing success message.
}
?>

Now this is overly simplified. But basically it is saying that if there has been no value set for the Name box, then print out an error message about that box and quit running. If the IP box is empty then it gives an error message about the IP box being empty and it will quit. If it passes both of those tests then it will interact with the DB.

But some more checks should be run in addition. You can go here: http://www.electrictoolbox.com/php-validate-ip-address-filter-var/ for examples of how to add another elseif statement that will check that what is sent in the IP box is actually a possible IP address. It will make sure that someone doesn't enter "frank" or "2nh4ie8" in there. It won't necessarily check that it is the true IP address but it will at least ensure that it is a a true possible IP address.

You'll want to validate AND filter/sanitize every bit of form input sent by a user possible before adding it to the database and/or printing them out to the browser.

Member Avatar
Verean
Newbie Poster
3 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I have inserted the code, but every time I submit it just says, "You must enter a name."

<?php
$con = mysql_connect("host","username","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("tablename", $con);

$Name = $_POST['NAME'];
$IP = $_POST['IP'];

if (!isset($Name)) {
echo "You must enter a name.";
exit;
}
elseif (!isset($IP)) {
echo "You must enter an IP address.";
exit;
}

$sql="INSERT INTO servers (Name, IP)
VALUES
('$_POST[Name]','$_POST[IP]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added.";

mysql_close($con)
?>
Question Answered as of 2 Years Ago by pritaeas and weekendrockstar
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article