javascript with php..

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

javascript with php..

Post 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">
User avatar
R4000
Forum Contributor
Posts: 168
Joined: Wed Mar 08, 2006 12:50 pm
Location: Cambridge, United Kingdom

Post 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.
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post 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 :)
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post 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[]
Ocendo
Forum Newbie
Posts: 3
Joined: Tue Mar 07, 2006 7:55 pm

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