473,395 Members | 1,471 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

JavaScript and PHP var problem

Hi

I'm trying to get a small script working I need to get the user to
input some info from a prompt and then use that info in
a PHP script, but I can't seem to assign the JavaScript var
to a PHP var

<?PHP

if ($do == 'ban') {
print "<SCRIPT language=JavaScript>";
print "var reason; ";
print 'reason=prompt("Please enter the reason for banning this
user:","");';
print "document.write(\"Banning ID $id - \"+reason+\"<br>\");";

/// problem here none of these work

print "document.write(\"$banreason =\"+reason+\"\");";
print "$banreason = reason";
////
print "</SCRIPT>";
print "outside javascrpt the reason = $banreason<br>";
// do PHP SQL here
}

?>

Jul 23 '05 #1
3 2328
Brian wrote:
Hi

I'm trying to get a small script working I need to get the user to
input some info from a prompt and then use that info in
a PHP script, but I can't seem to assign the JavaScript var
to a PHP var
Thats because by the time the prompt has appeared, the PHP script on the
server is finished processing. If you need to get info back to the
server, submit a form, let PHP handle the form. JS and PHP can't
communicate the way you are trying to do it.
<?PHP

if ($do == 'ban') {
print "<SCRIPT language=JavaScript>";
print "var reason; ";
print 'reason=prompt("Please enter the reason for banning this
user:","");';
print "document.write(\"Banning ID $id - \"+reason+\"<br>\");";

/// problem here none of these work

print "document.write(\"$banreason =\"+reason+\"\");";
print "$banreason = reason";
////
print "</SCRIPT>";
print "outside javascrpt the reason = $banreason<br>";
// do PHP SQL here
}

?>

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #2
Brian wrote:
Hi

I'm trying to get a small script working I need to get the user to
input some info from a prompt and then use that info in
a PHP script, but I can't seem to assign the JavaScript var
to a PHP var

<?PHP

if ($do == 'ban') {
print "<SCRIPT language=JavaScript>";
print "var reason; ";
print 'reason=prompt("Please enter the reason for banning this
user:","");';
print "document.write(\"Banning ID $id - \"+reason+\"<br>\");";

/// problem here none of these work

print "document.write(\"$banreason =\"+reason+\"\");";
print "$banreason = reason";
You can not set a server-side variable from client-side JavaScript[1].
PHP reads and parses the code above and generates HTML (which may or
may not include client-side JavaScript). That HTML is sent to the
client to be rendered, ignored, saved to disk, whatever. It does not
matter, by the time the client sees _ANY_ of the client-side
JavaScript you have written, the server has already forgotten about
that request and moved on to other tasks. To say it another way, PHP
is _not_ processed a line at a time with the client and server
interacting in any way. PHP parses the entire file and sends the
result to the client.
////
print "</SCRIPT>";
print "outside javascrpt the reason = $banreason<br>";
// do PHP SQL here
}

?>


[1] this is not completely true, there are ways of getting client-side
values to the server, but not the way you are attempting to do, and
not while the page is being parsed by PHP. For example, you could have
something like:

<script type="text/javascript">
function saveInDatabase(someVariable) {
var phpDispatcher = new Image();
phpDispatcher.src =
'http://someserver/saveInDatabase.php?theValue=' + someVariable;
}
</script>
<a href="#" onclick="saveInDatabase(prompt('Enter a value to save in
the database'));return false;">Click here</a>

but this concept can _not_ be used to return a value to the server for
use in the same page it is currently parsing.

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq

Jul 23 '05 #3
Thanks Guys

Time for a re-think :) I feel some cookies coming on :)

Brian
Hi

I'm trying to get a small script working I need to get the user to
input some info from a prompt and then use that info in
a PHP script, but I can't seem to assign the JavaScript var
to a PHP var

<?PHP

if ($do == 'ban') {
print "<SCRIPT language=JavaScript>";
print "var reason; ";
print 'reason=prompt("Please enter the reason for banning this
user:","");';
print "document.write(\"Banning ID $id - \"+reason+\"<br>\");";

/// problem here none of these work

print "document.write(\"$banreason =\"+reason+\"\");";
print "$banreason = reason";
////
print "</SCRIPT>";
print "outside javascrpt the reason = $banreason<br>";
// do PHP SQL here
}

?>

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Kai Grossjohann | last post by:
It seems that Ctrl-N in Mozilla opens a new empty browser window. That's fine, I don't need to do anything about it. But Ctrl-N in IE appears to clone the current window. Is there a way to...
53
by: Cardman | last post by:
Greetings, I am trying to solve a problem that has been inflicting my self created Order Forms for a long time, where the problem is that as I cannot reproduce this error myself, then it is...
6
by: Alex Rast | last post by:
First of all, this is not a programming question. I'm a user, not programming in JavaScript. I'm not, however, a novice user or even a power user - I certainly know programming intimately as well...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.