Hello I have developed a form , i want the user to only input number if a user input any alphabet or symbol ad submit the form the error must appear on the same page,
i have done this with form action but i want the error to be shown on the same page not to the new page.
btw, i have used is_numeric function to detect the numeric
here is the form,

<form method="post" action="sign.php">
	<input type="text" name="first">
	<select name="select">
	<option>+</option>
	<option>-</option>
	<option>x</option>
	<option>/</option>
	<option>%</option>
	<input type="text" name="second">
	<input type="submit" name="submit" value="submit">
	</form>

Recommended Answers

All 20 Replies

In short, this is what you do:

sign.php

$error = 0;
if(!is_numeric($val1))
	$error += 1;

if(!is_numeric($val2))
	$error += 2;

if($error != 0)
	header("Location: form.php?error=$error");

This sends the user back to the form and passes information about which field wasn't a numeral.
1 (01): The first is not a numeral
2 (10): The second is not a numeral
3 (11): Both are not a numeral
Note: Make sure there is NO output before the header function or you will get an error.

form.php

<form method="post" action="sign.php">
	<input type="text" name="first">
	<?php echo ($_GET['error'] & 1) ? "Invalid input!" : ""; ?>
	<select name="select">
	<option>+</option>
	<option>-</option>
	<option>x</option>
	<option>/</option>
	<option>%</option>
	<?php echo ($_GET['error'] & 2) ? "Invalid input!" : ""; ?>
	<input type="text" name="second">
	<input type="submit" name="submit" value="submit">
	</form>

Hope this is understandable.

i am newbie and didn't understand your code at all , i think you didn't understand my question,
i want..
when a user enter non numeric value and submit the form then error must be shown on the same index page and if the user entered both the numeric value the result must be shown on the next page.

That is exactly what my code does.

If you however don't want the form to submit at all you obviously shouldn't use PHP to do the checking but JavaScript.
In that case, however, remember that JavaScript can be switched off and you'd still have to use PHP to check for numerals which means using my code.

Still I think it's very wise of you to say that I didn't understand your question when you don't understand the answer.


I'll try to break my code down to your level by providing an example.

The user enters:

first: a
second: b

My code on sign.php will do this:

$error = 0;
Is first a number? - No => $error += 1 //So $error is now 1
Is second a number? - No => $error += 2 //So $error is now 3
Is $error non-zero? - Yes => Send the browser back to the form but pass the error number $error along

On the form my code now produces the following:

//$_GET['error'] = 3;
Is the first bit set in $_GET['error']? - Yes => Print 'Invalid input!' after the first input box
Is the second bit set in $_GET['error']? - Yes => Print 'Invalid input!' after the second input box

Considering your response you probably don't know that 1 is written as 01 in binary, 2 as 10 and 3 as 11 which we use here to transfer the information about the input back to the form page.


I hope this is easy enough for you.

commented: <3 ! Xufyan +3

ok i've understood the programm , but what is $GET_error ?? and what is header("Location: form.php?error=$error"); ???
(sorry i am asking a silly question but i am still a newbie don't know much about php )

and when i run your code i got this error


Notice: Undefined index: error in E:\xampp\htdocs\test\form.php on line 3
Notice: Undefined index: error in E:\xampp\htdocs\test\form.php on line 10

That is exactly what my code does.

If you however don't want the form to submit at all you obviously shouldn't use PHP to do the checking but JavaScript.
In that case, however, remember that JavaScript can be switched off and you'd still have to use PHP to check for numerals which means using my code.

Still I think it's very wise of you to say that I didn't understand your question when you don't understand the answer.


I'll try to break my code down to your level by providing an example.

The user enters:

first: a
second: b

My code on sign.php will do this:

$error = 0;
Is first a number? - No => $error += 1 //So $error is now 1
Is second a number? - No => $error += 2 //So $error is now 3
Is $error non-zero? - Yes => Send the browser back to the form but pass the error number $error along

On the form my code now produces the following:

//$_GET['error'] = 3;
Is the first bit set in $_GET['error']? - Yes => Print 'Invalid input!' after the first input box
Is the second bit set in $_GET['error']? - Yes => Print 'Invalid input!' after the second input box

Considering your response you probably don't know that 1 is written as 01 in binary, 2 as 10 and 3 as 11 which we use here to transfer the information about the input back to the form page.


I hope this is easy enough for you.

header("Location: form.php?error=$error");

This is to redirect the user to the same page..for more reference about header,click here...
And moreover...the code above is not required at all...
Just use javascript for the same....Click here to learn about different kinds of validation(These are called validations i.e., numeric input,or alha-numeric input etc)

commented: thumbs up <3 xufyan +3

If the link to the page is something.php?error=insertsomethinghere
then $_GET = insertsomethinghere

Header is explained by IIM and the errors are due to not checking whether the variables are set

isset($_GET['error'])

I only provided you with a basic outline of how you would program what you requested, I'm not here to do your work for you.

header("Location: form.php?error=$error");

This is to redirect the user to the same page..for more reference about header,click here...
And moreover...the code above is not required at all...
Just use javascript for the same....Click here to learn about different kinds of validation(These are called validations i.e., numeric input,or alha-numeric input etc)

If you read my earlier answer you wouldn't have mentioned JavaScript as it can be turned off and then where's your validation?

ok i've understood the programm , but what is $GET_error ?? and what is header("Location: form.php?error=$error"); ???

If you have read this line above,you wouldn't have asked this ....
And about validations,it's better to use javascript or JQuery for this ,than using PHP because it must be done on client side otherwise it will just increase the load on your server.These are some of the basics that must be taken into considerations while programming.....
Hopefully it's clear...

If you have read this line above,you wouldn't have asked this ....
And about validations,it's better to use javascript or JQuery for this ,than using PHP because it must be done on client side otherwise it will just increase the load on your server.These are some of the basics that must be taken into considerations while programming.....
Hopefully it's clear...

Since we don't even know what his program is intended for I will take your comment as if you mean to apply it to every website that takes input from the user.

A simple counter-example would be a money booking site: What if someone disables JavaScript and inputs a value of -1000 to be booked from his account to someone else. Do you really think server load is what you should be worried about in that case?

I already mentioned above that ,if javascript is not a cool option ,then go for jquery.
Traffic control is must because otherwise it may reault to the server down.

I already mentioned above that ,if javascript is not a cool option ,then go for jquery.
Traffic control is must because otherwise it may reault to the server down.

Do you know that jQuery is actually a JavaScript framework and therefore if you switch off JavaScript you also switch of jQuery?
I guess you didn't.

@insensus-Just check any site for validation....You will understand why it is necessary to use this...
Also,you can read above ,there is no such question about javascript is enabled or not....

Member Avatar for diafol

Guys, guys, stop it or you'll give yourselves nosebleeds.

The thread starter admits he's a noob, so he doesn't understand the php solution - nice though it is. I agree that if this is a trivial bit of code, js would fit the bill - much easier and quicker than involving the server. However, as we don't know about the poster's wishes for this code, we can't assume that js will be acceptable.

So, Xufyan, is javascript acceptable? Will some of your users be likely to have JS turned off? If so, use the php solution. If not, you'd *probably* be better off with js. You can validate client-side no problem - shouldn't be an issue if the data isn't going to the server.

BTW, I think jQuery would be overkill for such a basic calculator, even though it's only the size of a small image.

My 2p.

I would suggest:
1. Use $_SERVER for your action
2. At the top of your file put something like
if(your_form_is_submitted){
//do your stuffs
}
3. Validate using REGEX and PHP's preg_match
$match = "/[0-9]/";
$input_from_form = $_POST;
if(!preg_match($match, $input_from_form)){
//echo error here
}

The thread starter admits he's a noob, so he doesn't understand the php solution - nice though it is.

I hope I have not added to confusion :)

Member Avatar for diafol

the regex for one or more digits is /^\d+$/

It's not important if the user turn-off javascript or not. You might check from the server-side at least for the security reasons like mysql injection.

commented: xufyan :) <3 +3
Member Avatar for diafol

if using js, server not involved, therefore no mysql.
if using php, can't see mysql being involved anyway for a simple calculator.

well i wanted to use php not javascript..! and both of you thanks for your great support!!!
!!!
thumbs up !!!

the regex for one or more digits is /^\d+$/

I'm trying to catch up with latest news on REGEX.
Thanks for correction :)

if using js, server not involved, therefore no mysql.
if using php, can't see mysql being involved anyway for a simple calculator.

May be storing answers blah blah? But then it will not be "simple calculator"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.