0

This is my code, I get variable from textbox but nothing appears.

This code checks if a input is number:

this is a number

or not:

this is not a number

<?php
echo'<form method="post" action="">';
echo '<input type=text name=t/>';
echo'<input type=submit name=su/>';
echo'</form>';
if(isset($_POST['su']))
{
    if (ctype_digit($_POST['t'])) {
        echo "This is number.\n";
    } else {
        echo "This is not a number.\n";
    }
}
?>

All of code is in one page.

  • Possible duplicate of [What is the default form HTTP method?](http://stackoverflow.com/questions/2314401/what-is-the-default-form-http-method) – Sean Nov 15 '15 at 21:21
  • 1
    You are getting no output when you submit the form? Works for me, https://eval.in/468977. Is the form processing; values submitting? I prefer quoted attribute values but I don't think that would cause your issue. – chris85 Nov 15 '15 at 21:35
  • @chris85 i need in current page check if input is positive number or not. do u know another way? – Sajad Khammar Nov 15 '15 at 21:42

3 Answers3

3

Your code is failing because you have unquoted elements/attributes.

The following require quotes for:

echo '<input type=text name=t/>';
                  ^^^^ ^^^^^^
echo'<input type=submit name=su/>';
                 ^^^^^^ ^^^^^^^

Modify your code to read as:

<?php
echo'<form method="post" action="">';
echo '<input type="text" name="t"/>';
echo'<input type="submit" name="su"/>';
echo'</form>';
if(isset($_POST['su']))
{
    if (ctype_digit($_POST['t'])) {
        echo "This is number.\n";
    } else {
        echo "This is not a number.\n";
    }
}
?>
  • Certain web browsers will not accept unquoted elements/attributes and will simply be ignored, such as the current version of Firefox and will fail silently even with error reporting set to catch/display, strangely enough.
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
1

You can test for a number using the JS function isNAN. PHP processes on the server so once the page is loaded it is not available. Javascript is available though after the page loads. JS is client side and is executed on the browser. Here's a js solution:

var test = 1;
if(isNaN(test)) {
    alert('String');
} else {
    alert('number');
}

This would alert number.

Here's a longer write up on that JS function, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN.

chris85
  • 23,846
  • 7
  • 34
  • 51
0

If method is not defined in <form> then the default is GET. So

<form>

is the same as

<form method="GET">

If you want to use $_POST, you need to do

<form method="POST">
Sean
  • 12,443
  • 3
  • 29
  • 47