how to add only integer in php

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 2084 Views - Last Post: 24 March 2010 - 10:08 PM Rate Topic: -----

#1 yeney_fiza  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-April 09

how to add only integer in php

Posted 10 March 2010 - 08:32 PM

hello, i have a problem to assign only integer number in php..i would like to get a number on Tarikh that will user only enter number not character. below is my code, please help me...

<?php

$page_title ='Kelulusan Jktk';
include('./includes/home.html');


if(isset($_POST['submitted']))
{
  $errors = array(); // create an empty new variable.

  // check for a No Rujukan
if (empty ($_POST ['Rujukan']))  {
  $errors[] = '<b>No Rujukan</b>';
  } else {
    $Rujukan = trim($_POST['Rujukan']);
    }

  //check for a Tkh Minit JKTK.
if (empty ($_POST ['Tarikh'])) {
  $errors[]  = ' <b>Tkh Minit JKTK</b>';

} else {
  $Tarikh = trim($_POST ['Tarikh']);
  }

  //check for an Rujukan Minit.
if (empty ($_POST ['Minit'])) {
  $errors[] = ' <b>Rujukan Minit</b>';
} else {
  $Minit = trim($_POST ['Minit']);
  }

  //check for a Status.
if (empty ($_POST ['Status'])) {
  $errors[] = ' <b>Status </b>';
} else {
  $Status = trim($_POST ['Status']);
  }

  if(empty($errors)) {

  require_once('mysql_connect.php');

  $query = "INSERT INTO jktk (Rujukan, Tarikh, Minit, Status)
            VALUES ('$Rujukan','$Tarikh','$Minit','$Status')";

  $result = @mysql_query($query);
  if($result) {

    echo '<p> <b>Laporan</b></p>';
    echo "<table border=\"2\" cellspacing=\"2\" cellpadding=\"2\">";
    echo "<tr><td><b>No Rujukan :</b></td><td><font color=\"blue\"><b>$Rujukan</b></font><br /></td></tr>";
    echo "<tr><td><b>Tkh Minit JKTK :</b></td><td><font color=\"blue\"><b>$Tarikh</b></font><br /></td></tr>";
    echo "<tr><td><b>Rujukan Minit :</b></td><td><font color=\"blue\"><b>$Minit</b></font><br /></td></tr>";
    echo "<tr><td><b>Status :</b></td><td><font color=\"blue\"><b>$Status</b></font><br /></td></tr>";
    echo "</table>";
    echo "<form action=\"Kelulusan Jktk.php\" method=\"post\">";
    echo "<p><b>Complete.</b></p>";
    echo "<input type =\"submit\" value=\"Confirm\" name =\"submit\">";
    echo "</form>";
    exit();

  } else {

  echo '<p><b> System error!</></p>';
  echo '<p> <b>you could not regiter due system error</b></p>';
  echo '<p>' .mysql_error(). '<br/><br/> Query : '.$query.' </p>';
  echo '<p> <b>please try again later </b></p>';
  exit();
  }

  mysql_close();

  }else{

    echo '<p> <b>Tidak Lengkap</b></p>';
    foreach($errors as $msg) {
      echo "- $msg<br/>\n ";

    }
   echo '<p><b>Kemas Kini</b></p>';
  }
}
?> <P>

ADD/EDIT KELULUSAN JKTK
<p>
<form action="Kelulusan Jktk.php" method="post">
<table border="2" cellspacing="2" cellpadding="2">
	<tbody>
		<tr>
			<td align="left"><font color="#0000ff"><strong><font face="georgia,palatino">No Rujukan</font></strong></font> <br />
			</td>
			<td align="left"><?php
  require_once('mysql_connect.php');

$result = @mysql_query("select awalId,Rujukan from awal");

print " <select name = \"awalId\"> \n";
while
($row = mysql_fetch_assoc ($result))
{
$awalId = $row ['awalId'];
$Rujukan = $row ['Rujukan'];
print " <option value = $awalId >$Rujukan\n";
}
print " </select> \n";
print "</p> \n";
?>
			</td>
			</tr>
			<tr>
      <td align="left"><font color="#0000ff"><strong><font face="georgia,palatino">Tkh Minit JKTK</font></strong></font> <br />
			</td>
			<td align="left"><input type="text" name ="Tarikh" size ="10" value = "<?php if(isset($_POST['Tarikh'])) echo $_POST['Tarikh'];?>" />dd/mm/yy
			</td>
      </tr>
      <tr>
      <td align="left"><font color="#0000ff"><strong><font face="georgia,palatino">Rujukan Minit</font></strong></font> <br />
			</td>
			<td align="left"><input type="text" name ="Minit" size ="30" value ="<?php if(isset($_POST['Minit'])) echo $_POST['Minit']; ?>" />
			</td></tr>
			<tr>
      <td align="left"><font color="#0000ff"><strong><font face="georgia,palatino">Status</font></strong></font> <br />
			</td>
			<td align="left"><input type ="radio" name ="on1" value ="Tutup">TUTUP
			<input type ="radio" name ="on2" value ="Buka">BUKA
			<input type ="radio" name ="on3" value ="KIV">KIV
			</td></tr>
			<tr>
     <td></td>
      <td><center><input type ="submit" value="Awal" name = "B1">
      <input type = "hidden" name ="submitted" value ="TRUE"/>
      <input type ="reset" value="Discard" name = "B2"> </center>
      </tD></tr>
      </table>
      
      <a href="Laporan Kelulusan Jktk.php">LAPORAN</a>


This post has been edited by no2pencil: 10 March 2010 - 08:33 PM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: how to add only integer in php

#2 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5189
  • View blog
  • Posts: 26,896
  • Joined: 10-May 07

Re: how to add only integer in php

Posted 10 March 2010 - 08:34 PM

View Postyeney_fiza, on 10 March 2010 - 09:32 PM, said:

that will user only enter number not character.

I would validate user input with Javascript. If you validate it in PHP, you have to pull the data back to process it. Javascript will function on the viewers browser. Thus only the data will only pull back once it's validated. Less back & forth on your server/script.
Was This Post Helpful? 0
  • +
  • -

#3 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: how to add only integer in php

Posted 10 March 2010 - 08:38 PM

The only downside to Javascript validation is knowing what to do if the user has Javascript disabled in their browser.
Was This Post Helpful? 0
  • +
  • -

#4 yeney_fiza  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-April 09

Re: how to add only integer in php

Posted 10 March 2010 - 08:42 PM

it their another way..
Was This Post Helpful? 0
  • +
  • -

#5 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 714
  • Joined: 03-August 06

Re: how to add only integer in php

Posted 10 March 2010 - 10:39 PM

The function ctype_digit will check a string for all numbers. Even if you validate with javascript, ultimately, before you accept the data you have to check it on your server to ensure its safe if you plan to store it somewhere.
Was This Post Helpful? 0
  • +
  • -

#6 Xuhybrid  Icon User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 106
  • Joined: 20-January 10

Re: how to add only integer in php

Posted 11 March 2010 - 05:02 AM

if (is_numeric($_POST['...']))
{
    
}

Was This Post Helpful? 0
  • +
  • -

#7 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 714
  • Joined: 03-August 06

Re: how to add only integer in php

Posted 11 March 2010 - 01:44 PM

According to the OP he's posted only integers. is_numeric also allows floats, so I would have to advise against using it.
Was This Post Helpful? 0
  • +
  • -

#8 garfinkle  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 27
  • View blog
  • Posts: 87
  • Joined: 08-September 09

Re: how to add only integer in php

Posted 11 March 2010 - 05:21 PM

for php use

<?php
if(is_int())
{
   // do stuff
}
?>


for javascript use


<script type="text/javascript">
<!--

function checkForInt(value)
{
   if(!isNAN)
   {
   //do stuff, this will check for numeric values
   // do a substring check for a decimal to eliminate floats 
   }
}

-->
</script>




Hope this helps
Was This Post Helpful? 0
  • +
  • -

#9 KidD1988  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 12-March 10

Re: how to add only integer in php

Posted 12 March 2010 - 02:51 AM

http://php.net/manua....is-numeric.php

$string = 123124;

if(is_numeric($string))
{
    echo('$string only contains numbers');
}
else
{
    echo('$string only numbers are allowed');
}



hope it helps, if(NOT){ i will be willing to help u out some more}else { return false } :P


LoL didnt read the comments befor i did my post, but another comment would be to stay away from JS since it can be disabled in the browser, Javascript is bad for validation

This post has been edited by KidD1988: 12 March 2010 - 02:59 AM
Reason for edit:: Added code tags

Was This Post Helpful? 0
  • +
  • -

#10 ansh9d  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-March 10

Re: how to add only integer in php

Posted 12 March 2010 - 06:50 AM

/// Function to check number
function chk_number($digit)
{
if(!is_numeric($digit))
{
    return false;
}
return true;
}

/// For checking 
$temp = '5858';
if (chk_number($temp){
    echo 'Number is numeric.';
}
else {
    echo 'Number is not numeric';
} 

This post has been edited by ansh9d: 12 March 2010 - 06:52 AM

Was This Post Helpful? 0
  • +
  • -

#11 Xuhybrid  Icon User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 106
  • Joined: 20-January 10

Re: how to add only integer in php

Posted 15 March 2010 - 02:04 AM

View Postansh9d, on 12 March 2010 - 05:50 AM, said:

/// Function to check number
function chk_number($digit)
{
if(!is_numeric($digit))
{
    return false;
}
return true;
}

/// For checking 
$temp = '5858';
if (chk_number($temp){
    echo 'Number is numeric.';
}
else {
    echo 'Number is not numeric';
} 

You are just bloating your code with an unecessary clone of an existing function.
Was This Post Helpful? 0
  • +
  • -

#12 mscobb  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 20-March 10

Re: how to add only integer in php

Posted 23 March 2010 - 01:13 PM

In PHP you can use the regular expression preg_match to check if it is all numbers or not.
Was This Post Helpful? 0
  • +
  • -

#13 Xuhybrid  Icon User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 106
  • Joined: 20-January 10

Re: how to add only integer in php

Posted 23 March 2010 - 04:54 PM

Yes true, but regex is significantly slower than is_numeric.
Was This Post Helpful? 0
  • +
  • -

#14 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2895
  • View blog
  • Posts: 10,028
  • Joined: 08-August 08

Re: how to add only integer in php

Posted 23 March 2010 - 05:01 PM

You can also force type:
if($x === (int)$x)
  echo "Yes!";
else
  echo "Bad data";


Was This Post Helpful? 0
  • +
  • -

#15 AdaHacker  Icon User is offline

  • Resident Curmudgeon

Reputation: 452
  • View blog
  • Posts: 811
  • Joined: 17-June 08

Re: how to add only integer in php

Posted 23 March 2010 - 06:50 PM

View PostCTphpnwb, on 23 March 2010 - 06:01 PM, said:

You can also force type:
if($x === (int)$x)

Good thought, but that's actually not going to work. At least, not with user input. Values from $_GET and $_POST will always come through as a string type, so because of the triple-equal, the types will never match and the comparison will always fail. That only works if you've already converted $x to an integer type, at which point you wouldn't need this snippet in the first place.

Using a double-equal wouldn't automatically fail on user input, but not right either. Because integer casting truncates floats and chops off strings after the first non-digit, if $x is something like 1.0 or "123acb" then the expression would still evaluate to true.

The best solution (unless you're one of the cursed souls who can't use PHP 5.2) would simply be to use the filter extension. That's what it's there for. You can check this with a simple call to filter_var(). It correctly handles all type of strings and floats and even returns the value as an the correct data type.
if (filter_var($x, FILTER_VALIDATE_INT) === false) {
   echo "Bad data - not an int";
} else {
   echo "OK - it's an integer";
}


This post has been edited by AdaHacker: 23 March 2010 - 06:50 PM

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2