Forum Moderators: coopster

Message Too Old, No Replies

passing variables javascript/php

variables javascript php

         

rdunne

8:21 pm on Mar 18, 2007 (gmt 0)

10+ Year Member



I am trying to execute php script using a button on a web page. I understand the onclick() function expects javascript. Can I declare the script type as php or do I need to run a php script within the javascript? I have the php running a database query, the result of which I want to assign to a text field(on the same page). On a related issue, when I try to assign a php $variable to a html text field all I see is the php code in the field. Any geniuses out there know how to get around these problems?

eelixduppy

8:49 pm on Mar 18, 2007 (gmt 0)



Welcome to WebmasterWorld, rdunne!

In order to parse a php file by clicking a button you have to use some form of Ajax. Rasmus has a great little introduction to the topic in his 30 Second Ajax Tutorial [news.php.net]. You might also want to take a look at the HttpRequest Object [w3schools.com] as it directly relates.

>> all I see is the php code in the field

You are seeing this because the php code isn't being parsed by the server. You have to remember that it is a server-side technology and cannot be interpreted by the browser, therefore it displays it as plain text. In order for php code to be executed, the server must process it, and then send the results back to the browser.

If you need further help on this issue, don't be afraid to ask! :)

Good luck!

grandpa

1:35 pm on Mar 19, 2007 (gmt 0)

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



Hi rdunne.

What you want to do.. some of it, sounds similar to what I did in one of my scripts. I didn't need ajax as a solution, or maybe it wasn't called ajax back then ;) Without posting your entire script, can you give us an example of what you are attempting?

capulet_x

1:43 am on Mar 25, 2007 (gmt 0)

10+ Year Member



I'm not certain is this will help and I am by NO MEANS an expert but below is part of a form that was included IN a PHP script. The script took variables that were posted to it from a form in FLASH and then was automatically submitted using the java script below (...by the way I'm sure there are more elegant ways of doing this) I guess the point here is that you can see below how your PHP variables might be included in your HTML forms.

look for:
<? echo $service;?>
below.

If this is no help at all I hope at least it gets the wheels turning again in a direction that will help you.

<form method="POST" name="testform" action="goto_some_other_URL.php">
<table align="center">
<tr valign="baseline">
<td nowrap align="right"></td>
<td><input type="hidden" name="clients" value=" 1 " size="32"></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">&nbsp;</td>
<td><input type="hidden" value="<? echo $service;?>" size="32"></td>
</tr>
</table>

<input type="hidden" name=submit" value="testform">
</form>

<script type="text/javascript" language="JavaScript"><!--

document.testform.submit();

//--></script>