Passing a variable's value from javascript to a perl script

S

satish2112

Hi,

I have a text-area which contains values from mysql database and 2
buttons, Edit and Update.
When I click on the Edit button, I can edit the text-area
(initially non-editable).
After this, if I click on the Update button, the values in the
text-area must be updated in the mysql database.
I am storing the values of the text-area in a variable. I am using
javascript to pass the variable.
How do I pass this variable to a Perl script and how do I recieve
that variable in that script?

Please help me!!!
 
A

ASM

En réponse à (e-mail address removed) qui écrivit, en date du : 4/09/07
11:40, le message suivant :
(...)
I am storing the values of the text-area in a variable. I am using
javascript to pass the variable.

Why ?
The form can't send this by itself ?
How do I pass this variable to a Perl script and how do I recieve
that variable in that script?

It is a PHP question, no ?
(or an asp question ...)

<form action="myFile.asp" method="post">
<textarea name="content" ...></textarea>
<button ....>Edit</button>
<input type=submit value="Save">
</form>

And with multiple textareas

<form action="myFile.asp" method="post">
<p>Infos 1 :
<textarea name="Text_1" ...></textarea>
<button ....>Edit</button>
<input name="save" type=submit value="Save info 1">
</p>
<p>Infos 2 :
<textarea name="Text_2" ...></textarea>
<button ....>Edit</button>
<input name="save" type=submit value="Save info 2">
</p>
<p>Infos 3 :
<textarea name="Text_3" ...></textarea>
<button ....>Edit</button>
<input name="save" type=submit value="Save info 3">
</p>
<p><input name="save" type=submit value="Save all"></p
</form>

The variables 'content'
or 'T_1' 'S_1' 'T_2' 'S_2' 'T_3' 'S_3'
are sent with their values
your asp (or php) has to analyze these couples variable+value



If you absolutly want to sent both to php and to perl

<form action="myFile.php" method="post"
onsubmit="return aspJS(this);">
<script type="text/javascript">
function aspJS() {
var url=[], f = this.elements;
for(var i=0; i<f.length; i++)
if(f.type=='textarea' && !f.disabled)
url[url.length] = f.name+'='+f.value;
if(url.length<1) {
alert('no change !');
return false;
}
setTimout(function() {
location.href = 'myFile.asp?'+url.join('&');
},10);
return true;
}
</script>

*if JS is enabled* that would call your asp and post the form to php
but ... what about disabled JS ?

--
 
B

Bart Van der Donck

I have a text-area which contains values from mysql database and 2
buttons, Edit and Update.
When I click on the Edit button, I can edit the text-area
(initially non-editable).
After this, if I click on the Update button, the values in the
text-area must be updated in the mysql database.
I am storing the values of the text-area in a variable. I am using
javascript to pass the variable.
How do I pass this variable to a Perl script and how do I recieve
that variable in that script?

Form with textarea:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/Transitional.dtd">
<html>
<head>
<title>Web page</title>
</head>
<body>
<form method="post" action="script.pl"
onSubmit="document.forms[0].hiddenvar.value
= document.forms[0].field.value">
<input type="hidden" name="hiddenvar">
<p><textarea cols="20" rows="20" name="field"></textarea></p>
<p><input type="submit" value="update"></p>
</form>
</body>
</html>

#!/usr/bin/perl
print "Content-Type: text/html; charset=iso-8859-1\n\n";
use strict;
use warnings;
use CGI;
my $query = new CGI();
print q{<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/Transitional.dtd">
<html>
<head>
<title>Web page</title>
</head>
<body> };
print '<p>Value of textarea: '.$query->param('field').'</p>';
print '<p>Value of hiddenvar: '.$query->param('hiddenvar').'</p>';
print '</body></html>';

Hope this helps,
 
T

The Natural Philosopher

Bart said:
I have a text-area which contains values from mysql database and 2
buttons, Edit and Update.
When I click on the Edit button, I can edit the text-area
(initially non-editable).
After this, if I click on the Update button, the values in the
text-area must be updated in the mysql database.
I am storing the values of the text-area in a variable. I am using
javascript to pass the variable.
How do I pass this variable to a Perl script and how do I recieve
that variable in that script?

Form with textarea:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/Transitional.dtd">
<html>
<head>
<title>Web page</title>
</head>
<body>
<form method="post" action="script.pl"
onSubmit="document.forms[0].hiddenvar.value
= document.forms[0].field.value">
<input type="hidden" name="hiddenvar">
<p><textarea cols="20" rows="20" name="field"></textarea></p>
<p><input type="submit" value="update"></p>
</form>
</body>
</html>

#!/usr/bin/perl
print "Content-Type: text/html; charset=iso-8859-1\n\n";
use strict;
use warnings;
use CGI;
my $query = new CGI();
print q{<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/Transitional.dtd">
<html>
<head>
<title>Web page</title>
</head>
<body> };
print '<p>Value of textarea: '.$query->param('field').'</p>';
print '<p>Value of hiddenvar: '.$query->param('hiddenvar').'</p>';
print '</body></html>';

Hope this helps,

The way i do this..not necessarily the best..is as follows.

A load of buttons, with optional onmouseover/onmouseout highlighting and
onclick properties are set up to call javascript functions onclick, or
onchange, depending what they are.

There are a plethora of <INPUT type="hidden" value="" name ="myname>
statements in the HTML code.


Heres an example fragement. Its generated BY php, but it illustrates how
to highlight and color in selected stuff from <SELECT><OPTION> stuff,
and on change the correct value gets stuffed into the
selected_product_id POST hidden variable.


The highlighting code is weird. Ie6 needed the color changing on each
option element, whereas safari and firefox were happy with the select
object getting colored in. So I did both.

The PHP ensures that every select option has a different VALUE assigned
to it, so all the onchange function in the selectors does, is read that
and stuff the selected one into the hidden variable.

The two buttons are done using a table element of class button, which
incorporates a transparent edged image of a button as a single centered
background image:-

..button
{
background-image: url("../Images/Button1.gif");
background-repeat: no-repeat;
background-position: center center;
}

Then the text is enclosed in a bold block with class that changes
onmouseover, and onmouseout, and onclick calls a slightly different
javascript function to either invoke the next program with an ID set, or
warn if no ID was set and the 'edit' button was pressed.




<FORM method="POST" enctype="multipart/form-data" action="project_edit.php">
<center>
<table cellpadding="10px">
<TR><TD align="left">
Select project to access by clicking or selecting it, (yellow when
selected) or select 'new' to create a new one.
</TD></TR></table>
</center>
<HR>
<script type="text/javascript" language="JavaScript">
function selectproject()
{
document.getElementsByName("select_project")[0].value="yes";
if(document.getElementsByName("selected_project_id")[0].value=="")
alert("You didn't select a project to edit!!\n");
else document.forms[0].submit();
}
function newproject()
{
document.getElementsByName("selected_project_id")[0].value="";
document.getElementsByName("select_project")[0].value="yes";
document.forms[0].submit();
}

function select_project_id(here)
{
index=here.selectedIndex;
for(i=0;i< document.forms[0].project_id.length;i++)
{
for (j=0;j<document.forms[0].project_id.length;j++)
document.forms[0].project_id.options[j].style.color="white";
document.forms[0].project_id.style.color="white";
}
here.style.color="yellow";
here[index].style.color="yellow";
document.getElementsByName("selected_project_id")[0].value=id;
}
</script>

<CENTER><TABLE border="0">
<TR><TD width="200px" height="55px" align="center" valign="center"
class="button" >
<b class="s3" onclick=" newproject()"
onmouseover="this.className='s4'"
onmouseout="this.className='s3'">
NEW PROJECT</b></TD>

<TD height="55px" align="center" valign="center" class="button" >
<b class="s3" onclick=" selectproject()" onmouseover="this.className='s4'"
onmouseout="this.className='s3'">
EDIT SELECTED</b></TD></TR>

<TR><TD align="center"><b>xxxx's Projects</b></td>
<TD align="center">
<select STYLE="width: 200px" name="project_id"
onclick="select_project_id(this)">
<OPTION value= "11" >More tests (Sep 4th 07)</OPTION>
<OPTION value= "8" >Database project (Sep 3rd 07)</OPTION>
</TD><TD></TD>
</TR>
<TR><TD align="center"><b>Mike Mouses Projects</b></td>
<TD align="center"><select STYLE="width: 200px" name="project_id"
onclick="select_project_id(this)">

<OPTION value= "5" >another test project (Sep 1st 07)</OPTION>
<OPTION value= "6" >Further tests (Aug 30th 07)</OPTION>
</TD><TD></TD></TR>

<TR><TD align="center"><b>Freds Projects</b></td>
<TD align="center"><select STYLE="width: 200px" name="project_id"
onclick="select_project_id(this)">
<OPTION value= "7" >Miles away (Sep 3rd 07)</OPTION>
<OPTION value= "4" >Test project (Sep 2nd 07)</OPTION>
</table>
<input type="hidden" name="selected_project_id" value="">
<input type="hidden" name="select_project" value="no">
</FORM>

The program this gets passed to examines the POST variable
selected_project_id and if it is non null, raises an editing screen for
hat project: If not it takes the user to a create new project screen.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top