Page 1 of 1

javascript with php..

Posted: Thu Mar 09, 2006 1:43 pm
by ol4pr0
Dont know why but this isnt working.

I dont see the input boxes at all when loading script.

Code: Select all

<?php
require_once($_SERVER['DOCUMENT_ROOT']."/Classes/mysql_class.php");
$dbc = new database( );
$dbc->connect() or die( $dbc->getError() );
function search($val) {
	global $dbc;
	$result = $dbc->select("stock",array("pid"=>$val)) or die( $dbc->getError() );
	foreach ($result as $row) {
		$val = $row->omschrijving;
	}
	return $val;
}
?>
</head>
<body>
<?php
echo "<script type=\"text/javascript\">\n";
echo "<!--\n";
echo "function copyValue(the_value){\n";
echo " document.getElementsByName(\"output\")[0].value = ".search("the_value");
echo "}";
echo "</script>";
?>
<input type="text" name="input" onChange="copyValue(this.value)"> 
<input type="text" name="output">

Posted: Thu Mar 09, 2006 1:49 pm
by R4000
It could be because your HTML isn't exactly 'neat'.

but also you can't do PHP and javascript like that...

You can create something similar using AJAX or something along the lines of AJAX.

If you wait a few hours, i'll have posted my AJAX tutorial.

Basicly what AJAX would do is.

1. User does something to trigger function 'copyValue' (in this case, onChange)
2. Server sends a hidden request to server and then parses response and throws the output back to javascript.
3. You make it do what you want with response... mainly place it in the second box.

Posted: Thu Mar 09, 2006 2:38 pm
by ol4pr0
No the html isnt very need. I could have it return a value by just using return $val with the php function but it seems that by throwing in the query it stopped doing what i wanted to do..

Thanks.. i would indeed like that.What ever it takes to make that piece working :)

Posted: Fri Mar 10, 2006 8:43 am
by ol4pr0
I tried some of the AJAX which is pretty need, however i think ( if i am correct ) that it can not be used to have it filled in multiple input boxes having the same name or id. they all have to be unique. so no array[]

Posted: Fri Mar 10, 2006 4:01 pm
by Ocendo

Code: Select all

echo "<script type=\"text/javascript\">\n"; 
echo "<!--\n"; 
echo "function copyValue(the_value){\n"; 
echo " document.getElementsByName(\"output\")[0].value = ".search("the_value"); 
echo "}"; 
echo "</script>";
change it to this:

Code: Select all

echo '<script type=\"text/javascript\">\n'; 
echo '<!--\n'; 
echo 'function copyValue(the_value){\n'; 
echo ' document.getElementsByName(\"output\")[0].value = ".search("the_value") '; 
echo '}'; 
echo '</script>';
You see anytime you use " inside an echo that you are using " to tell what in it, the php code will get confused. Therefor if you are using " in the echo, start the echo off with '.

That may be why you don't see any input boxes or anything.[/quote]