Page 1 of 2

2 Questions about PHP

Posted: Thu Oct 24, 2002 8:18 pm
by rax369
Please answer me 2 questions please: :P

1) There is some command, function or something else to allow only enter numbers inside a text field of a form :?: (dont want allow to enter characters, letters or words, 'cause I'm gonna use this variable later to make some calculations)

2) What PHP function use to call another php page :?:

Sometimes when I'm going through tutorials following its examples or excercises, I have noticed that they use include("page.php") to insert the code of page.php into the page which I'm working on.

But I dont know if I use include() will call another page as I need now.

Thx for ur answers php gurus :D

Posted: Thu Oct 24, 2002 8:21 pm
by hob_goblin
1) is_int()
2) exec() or include() or require()

more info

Posted: Thu Oct 24, 2002 8:28 pm
by phpScott
to follow alittle on hob_goblin
You have to use javascript validation to check if an form entry in a number and not letters or a mix if you want to catch the input before it goes back to the server.

Posted: Thu Oct 24, 2002 8:45 pm
by rax369
About the 1st. question, I mean a expecific HMTL instruction to turn the text field of the form, in a text field that allows to users, only type inside it numbers, and only integer numbers.

My text field is called quantity and I'm using it inside of a form where users specify the quantity of pc devices to be requested to some XX pc devices provider... and u know, u cant get 0.5 mouses 8O , 15.36 motherboards 8O , 7.5 processors :lol: ... etc. etc.

Got my idea ? :wink: So I need some kind of HTML validation instruction , to allow being typed only integers inside of those text fields.

Thx guys. :wink:

Posted: Thu Oct 24, 2002 9:11 pm
by RandomEngy
For this I really wouldn't reccomend clientside validation. Have the user be able to submit the data, but check it at the server, and throw back an appropriate error if they try to enter a bad number/word.

thx man

Posted: Thu Oct 24, 2002 11:50 pm
by rax369
thx for ur suggestion RandomEngy, I got ur point, so only server side validation should be performed in this case uhhh... ok

html

Posted: Fri Oct 25, 2002 12:21 am
by phpScott
by its definition html is only a mark up language so it doesn't come with any specific functions, methods, etc. If you have a bunch of fields client side is the way to go as it saves all those trips back to the server, if you want to double check you can do it server-side as well.

phpScott

Posted: Fri Oct 25, 2002 1:50 am
by rax369
then if I'd like to make the validation at the client side, how could I allow the user introduce in the text fields, :( only integers, no character, works, etc. only integers.

So that I'd be saving work to the server ? :wink:

Posted: Fri Oct 25, 2002 2:27 am
by volka
with onkeyup() you can attach a (javascript-)function to the keyup-event of many elements. The value of an input-field will already be updated when the function is called.
i.e.

Code: Select all

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<title>only digits</title>
<script language="JavaScript" type="text/javascript">
	function limitToDigits(oTextArea)
	&#123;
		if ( (nPos = oTextArea.value.length) != 0)
		&#123;
			while(oTextArea.value.charCodeAt(--nPos) < 48 || oTextArea.value.charCodeAt(nPos) > 57)
				;
			if (nPos != oTextArea.value.length-1)
				oTextArea.value = oTextArea.value.substr(0,nPos+1);
		&#125;
	&#125;
</script>
</head>
<body>
	<form method="POST" action="process.php">
		<input type="text" name="textinput" onkeyup="limitToDigits(this);" />
	</form>
</body>
</html>
but nevertheless a server-side check is mandatory to be sure.......
in regard to this thread: viewtopic.php?t=3408
I made the script html 4.01 compliant (but only this time ;) )

Quantity

Posted: Fri Oct 25, 2002 6:53 am
by AVATAr
Question: ¿How much that quantity?...

you can use a combobox with the numbers in it... (like you use for years, months, etc)... here you are dong sort of "client validation"

the only problem could be the amount... but i dont think somebody will buy 200 mouses...

hope it helps

thx... suggestion taken

Posted: Fri Oct 25, 2002 10:15 am
by rax369
yea, u r right AVATAr, the only problem is the amount.

but that suggestion u said, could be an option,

It's just I wouldnt like limiting this to some determinated number of items, but mmm... I dont know, I dont know how to 'filter' the user input at the client side in a not so complicated way (javascript could be an option I know, but since I dont know nothig about it, and I'm begining to learn PHP, dont wanna go to java script poll yet, till' I get more 'knowledge' about PHP... man, I'm just begining to learn PHP) :oops:

a little javascript

Posted: Fri Oct 25, 2002 1:23 pm
by phpScott
you could do something along these lines if the amounts are coming from the text fields.

Code: Select all

<script language = javascript>
function validateNums()
&#123;
f=document.formName;
&#1111;b]
if(f.qty1.value != "")
&#123;
    if(!isan(f.qty1.value))
    &#123;
          f.qty1.focus()
          break;
    &#125;
&#125;
&#1111;/b]
function isan(string) &#123;
    if (string.length == 0)
        return false;
    for (var i=0;i < string.length;i++)
        if ((string.substring(i,i+1) < '0') || (string.substring(i,i+1) > '9'))
            return false;
    return true;
&#125;
This should test to see if a value is an int or not.
The part in bold you just repeat over and over for all the textbox's you want to check.
If the only place you have textboxes on the page is for qty's then post agian and I can send you some more elegant code to do the job.
In you <form> tag do this

Code: Select all

<form action="somepage.php" method="post" onSubmit="validateNums()">
hope that helps.

a little javascript

Posted: Fri Oct 25, 2002 1:23 pm
by phpScott
you could do something along these lines if the amounts are coming from the text fields.

Code: Select all

<script language = javascript>
function validateNums()
&#123;
f=document.formName;
&#1111;b]
if(f.qty1.value != "")
&#123;
    if(!isan(f.qty1.value))
    &#123;
          f.qty1.focus()
          break;
    &#125;
&#125;
&#1111;/b]
function isan(string) &#123;
    if (string.length == 0)
        return false;
    for (var i=0;i < string.length;i++)
        if ((string.substring(i,i+1) < '0') || (string.substring(i,i+1) > '9'))
            return false;
    return true;
&#125;
This should test to see if a value is an int or not.
The part in bold you just repeat over and over for all the textbox's you want to check.
If the only place you have textboxes on the page is for qty's then post agian and I can send you some more elegant code to do the job.
In you form tag do this

Code: Select all

<form action="somepage.php" method="post" onSubmit="validateNums()">
hope that helps.

javascript

Posted: Fri Oct 25, 2002 1:40 pm
by AVATAr
some browsert may not javascript on... thats a problem on this cases.. thats why you should check on the server side for errors...

Re: a little javascript

Posted: Sun Oct 27, 2002 1:14 pm
by rax369
phpScott wrote: If the only place you have textboxes on the page is for qty's then post again and I can send you some more elegant code to do the job.
In you form tag do this

Code: Select all

<form action="somepage.php" method="post" onSubmit="validateNums()">
hope that helps.
hi phpScott, here I'm again, posting... yes, the only part in my page where I have textboxes is to enter the quantity of a product to be requested... could u please show me that "more elegant code" to do the job :D of allowing enter only integer inside my textboxes form.

Could u explain that code piece pretty well to understand every sentence what it does?, I barely understood ur last post where u posted the javascript code to do this, I guess that the part in bold u were talking about is the part between

Code: Select all

&#1111;b] and &#1111;/b]
right ?

Other question... I know (sorry if I'm wrong :oops: ) the declaration of javascript begins with <script language = javascript> and finish with: </script> but since I dont see where u finish the declaration of javascript with "</script>" in ur code I dont know where it finish to be declared.

Finally, to give u an idea of my page, look at this image, which is a little part of how it looks like:

Image

This is an old image of how my page looked at the begining, now I have a lot of textboxes to allow the user enter the quantity because I added more pc devices categories like processors, motheboards, video cards, sound cards, burners, cd-roms, etc. and after the drop down menu showing the product name with its respective price, there is always a textbox to enter the quantity of the device.

thx man ! :D