Forum Moderators: open

Message Too Old, No Replies

Converting PHP to Javascript

a form vaildation question

         

chambers

10:38 pm on Jul 4, 2007 (gmt 0)

10+ Year Member



I'm trying to turn a PHP question into a Javascript one.


else if(strcasecmp($person, $name) == 0 && $code!== $password ¦¦!empty($another_person) && $code!== $another_password )

Some of the values are taken from a form and some are taken from a database. I can already turn the form vaules into Javascript ones, but how do I make the database/php values work in Javascript?

Also, I'm guessing the question is pretty much the same in Javascript, but what of the strcasecmp function? I've looked around to see if there's a js equivalent but found nothing.

Is there Javascript resource site like php.net? Something that has the Javascript documentation.

Bernard Marx

2:20 am on Jul 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You just need the functions translated. According to the PHP manual [php.net], returning zero from this unction is essentially saying "are the same, ignoring case".

strcasecmp(person, name) becomes:

person.toLowerCase() = name.toLower.case()

There isn't simple, direct analogue to empty [php.net]. All JS values have a Boolean evaluation. This could be analogous, except,in JS, "0" and an empty array do not evaluate to false. So

!empty(another_person) could become:

!?another_person
(note: that's 2 exclamation marks - the forum software removes 2 in a row)

for most, but not all types.

[edited by: Bernard_Marx at 2:22 am (utc) on July 5, 2007]

Bernard Marx

2:27 am on Jul 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Concerning documentation, Mozilla's is getting quite good.

[mozilla.org...]

You want the Core Guide, and Core Reference. Note that there are some differences between Javacript & JScript, but you'll have to look hard to find them.

Bernard Marx

8:20 am on Jul 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



CORRECTION:

person.toLowerCase() == name.toLower.case()

chambers

8:46 pm on Jul 5, 2007 (gmt 0)

10+ Year Member



So I don't have to do anything to make the php variables work with js?

For example to use $anotherperson, I just drop the $ sign and use it in a js string?

chambers

9:41 pm on Jul 5, 2007 (gmt 0)

10+ Year Member



Okay, I've found out how to pass along the value.


<script>
var whatever = "<?= $phpVar?>";
</script>

These things always look so simple once you see it.

eelixduppy

9:57 pm on Jul 5, 2007 (gmt 0)



Just remember that javascript validation can be circumvented using various methods. I hope you aren't eliminating your server-side validation completely. Your best bet is to have both server- and client-side validation.

penders

1:08 pm on Jul 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



So I don't have to do anything to make the php variables work with js?

For example to use $anotherperson, I just drop the $ sign and use it in a js string?

You don't need to drop the '$' sign in JS. '$anotherperson' is perfectly valid, it's just that JS convention has been to not use '$' in vars. In PHP you must. I am seeing more and more JS that uses $'s - probably because they are coding in PHP as well.

Bernard Marx

2:13 pm on Jul 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No doubt dedicated PHP editors are different but my code editor treats $ like a bracket ora dot - it doesn't select it when double-clicking a word. That's reason enough for me not to use them. It would be like walking around with my shoelaces undone.

Actually, I believe that the $ sign has a structural significance in PHP that is meaningless in JS, because there is a fundamental difference in the way that identifiers are handled between the languages.

(PHP)

$a = 'foo';
$b = 'a';
echo $$b; // -> foo

Such jiggery pokery would be done differently in JS, where all "entities" can be passed around as data. Function names become variables in their containing scope. All variable names are really property names of some object.