0

This is a repeat question of Check $_POST['id'] is numeric then do this if not do otherwise

But..

The answers given do not answer the question IMHO.

The question is testing a $_POST string to see if it is a number or contains non-numeric characters. Not change the code to use $_GET.

The original question is as such:

if (!empty($_POST['id'])) { 
echo "empty";
} else {
if (is_numeric($_POST['id'])) {
echo "numeric!";
} else {
echo "not empty but not numeric how come?";
}
}

I have tried, is_numeric() , but since $_POST delivers the variable as a string, even if it is a number this is useless.

I have tried, if(!preg_match('#[^0-9]#',$id)), but I have no idea why this doesn't work.

ctype_digit() I believe still has a problem with seeing all things from $_POST as strings, so no good.

There has to be a way, but I'm lost on how to do this. And no I can't use $_GET.

UPDATE ANSWER!!! (TYPO!!!!! god dammit!!!!):

// test.php

// testing with $_POST['id'] from forum with id = 5 and another test where id = new

$id = $_POST['editid'] ;
echo "<br>---".$id."---<br>";

if (empty($id)) { 
echo "<br>1: empty";
} else {
if (!is_numeric($id)) {
echo "<br>2: This is the number 5";
} else {
echo "<br>3: the must be the word new";
}
}

 // test 2 ... ctype_digit


if (empty($id)) { 
echo "<br>4: empty";
} else {
if (!ctype_digit($id)) {
echo "<br>5: This is the number 5";
} else {
echo "<br>6: the must be the word new";
}
}

// test 3 ... 



if (empty($id)) { 
echo "<br>7: empty";
} else {
if (!preg_match('#[^0-9]#',$id)) {
echo "<br>8: This is the number 5";
} else {
echo "<br>9: the must be the word new";
}
}

/**

result from 5


---5---

3: the must be the word new
6: the must be the word new
8: This is the number 5

results from "new"



**/
Community
  • 1
  • 1
  • `is_numeric ` — Finds whether a variable is a number or a numeric string – Anik Islam Abhi Nov 02 '16 at 04:48
  • 1
    The value of `$_POST` and `$_GET` are both strings (unless specified otherwise). Why does the `preg_match` and/or `ctype` not work? Have you outputted the string to confirm it is as you expect? Neither the regex, nor `ctype` will accept a float. What are you passing in? – chris85 Nov 02 '16 at 04:53
  • The numbers passed in our mysql id table row numbers, except when a form data uses the word "new" to tell the script to create a new record. I've tested the code for is_numeric which to me should work, but doesn't (that's how I found the other question) ctype_digit had weird results, it jumped over all the logic. I'm going to write a test.php to see if it was some wierd typo or something that I did, but I simply don't know how to use it. –  Nov 02 '16 at 04:57
  • going to include this as an answer to the original question. –  Nov 02 '16 at 05:27
  • 1
    You have a negative logical operator in your if statements. Specifically saying if it is NOT numeric, then echo "this is numeric". Drop the `!` – Luke Nov 02 '16 at 05:52
  • And your preg_match is a double negative. So that _SHOULD_ work, – Luke Nov 02 '16 at 06:02
  • Your regular expression should be: #[0-9]+#. It means 1 or more instances of characters in the range of 0-9 – Nadir Latif Nov 03 '16 at 05:16

0 Answers0