2 Questions about PHP

JavaScript and client side scripting.

Moderator: General Moderators

User avatar
rax369
Forum Commoner
Posts: 56
Joined: Sun Oct 06, 2002 8:50 pm

2 Questions about PHP

Post 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
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

1) is_int()
2) exec() or include() or require()
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

more info

Post 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.
User avatar
rax369
Forum Commoner
Posts: 56
Joined: Sun Oct 06, 2002 8:50 pm

Post 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:
User avatar
RandomEngy
Forum Contributor
Posts: 173
Joined: Wed Jun 26, 2002 3:24 pm
Contact:

Post 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.
User avatar
rax369
Forum Commoner
Posts: 56
Joined: Sun Oct 06, 2002 8:50 pm

thx man

Post by rax369 »

thx for ur suggestion RandomEngy, I got ur point, so only server side validation should be performed in this case uhhh... ok
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

html

Post 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
User avatar
rax369
Forum Commoner
Posts: 56
Joined: Sun Oct 06, 2002 8:50 pm

Post 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:
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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 ;) )
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

Quantity

Post 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
User avatar
rax369
Forum Commoner
Posts: 56
Joined: Sun Oct 06, 2002 8:50 pm

thx... suggestion taken

Post 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:
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

a little javascript

Post 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.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

a little javascript

Post 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.
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

javascript

Post 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...
User avatar
rax369
Forum Commoner
Posts: 56
Joined: Sun Oct 06, 2002 8:50 pm

Re: a little javascript

Post 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
Post Reply