473,692 Members | 2,231 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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=JavaSc ript>";
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 2341
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=JavaSc ript>";
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.javas cript 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=JavaSc ript>";
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.s rc =
'http://someserver/saveInDatabase. php?theValue=' + someVariable;
}
</script>
<a href="#" onclick="saveIn Database(prompt ('Enter a value to save in
the database'));ret urn 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*****@agrico reunited.com>
comp.lang.javas cript 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=JavaSc ript>";
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
9439
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 intercept the key so that I can do stuff on the server side to make the new window behave correctly? (We have a JSP-based webapp which stores state in the session. Now if two windows access (and modify!) the same session, then madness will result....
53
5709
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 difficult to know what is going on. One of these Order Forms you can see here... http://www.cardman.co.uk/orderform.php3
6
2272
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 as computer hardware, configuration, etc... down to the lowest level (e.g. BIOS, assembly language, etc). Anyway, I've obviously got something wrong with JavaScript configuration, files, etc. in Windows 2000, Service Pack 3, because on sites...
136
9356
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 code was littered with document.all and eval, for example, and I wanted to create a practical list of best practices that they could easily put to use. The above URL is version 1.0 (draft) that resulted. IMO, it is not a replacement for the FAQ,...
0
8610
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9090
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8810
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6462
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5822
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4327
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4562
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2978
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
1961
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.