Page 1 of 1

how does this translate??

Posted: Fri Jul 30, 2004 1:30 am
by fresh
hey, wondering how this Jscript would translate into PHP:

Code: Select all

function chk0(){
    if(document.formsї0].a.value=="A") {
    document.formsї0].a.value = "Apple";
    } else {
    return;
    }}
    function chk1() {
    if(document.formsї0].b.value=="~") {
    document.formsї0].b.value = "~apple";
    } else {
    return;
    }}
Thanks in advance :)

Posted: Fri Jul 30, 2004 1:35 am
by feyd

Code: Select all

function chk0()
{
  if(isset($_GET['a']) && $_GET['a'] == 'A') return 'Apple';
  else return false;
}

function chk1()
{
  if(isset($_GET['b']) && $_GET['b'] == '~') return '~apple';
  else return false;
}
not a direct translation.. the code you posted is dynamically changing the value of a form element, which PHP cannot directly do without the data going to the server first..

Posted: Fri Jul 30, 2004 1:49 am
by fresh
so would I just go like this:

Code: Select all

$a = A
$b = ~
$ax = Apple
$bx = ~apple
$c = $_POST['a'];
$d = $_POST['b'];
if($c == $a) {
$c = $ax;
}  elseif($d == $b) {
$d = $bx;
}}
would that work... I'm wanting to change the form field dynamically like you said, and this is my attempt at that.. ;)

Posted: Fri Jul 30, 2004 2:00 am
by feyd
you'd have a parse error.. several times over..

~ is an operator, you need to quote the strings.

Posted: Fri Jul 30, 2004 2:26 am
by fresh
:oops: oops.. so if I add the quotes then that will change the value of the input box??

Posted: Fri Jul 30, 2004 2:30 am
by feyd
you'd have to echo out $c and $d respectively back into the form elements..

Posted: Fri Jul 30, 2004 2:47 am
by fresh
oh i just got what you meant thanks :)

P.s. how do I call a PHP function from an HTML page??

ex:

Code: Select all

<?php
$a = "A";
$b = "Apple";
$c = $_POST['a'];
function chk0() {
if($c == $a) {
echo "<input type='text' name='a' value=".$b."; maxlenght=60 onkeyup='chk0()'>";
} else {
return false;
}}
function chk1() {
$d = "~";
$e = "~apple";
$f = $_POST['b'];
if($f == $d) {
echo "<input type='text' name='a' value=".$e."; maxlenght=60 onkeyup='chk1()'>";
} else {
return false;
}}
?>
will this work like this, It isn't seeming to work for me, but this seems logical.. hmm.. could someone show me how to properly call those 2 php functions, I can't find anyway to make this work right... thanks :)

Posted: Fri Jul 30, 2004 3:16 am
by feyd
those calls will be to javascript, or the default scripting agent. The only way to communicate data to and from PHP is through submitting the data..

Posted: Fri Jul 30, 2004 3:41 am
by fresh
woo hoo, I got it working!! I feel good about this one, because I coded up a hybrid script, (my first one), and it works great.. thanks for the advice feyd :)

Posted: Fri Jul 30, 2004 8:06 am
by Grim...
<html> and Javascript is hybrid script, non?

Posted: Fri Jul 30, 2004 6:11 pm
by fresh
well, not really hybrid, but it is cool that I was able to tranlate a javascript function into PHP.. so to me it's kinda hybrid, because it utilises both PHP and Javascript, to process one event.. so that's why I said that :)