Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

post javascript variable and selectlist value simultaneously to php file

Posted on 2009-07-15
6
Medium Priority
?
216 Views
Last Modified: 2012-05-07
I have an html form with a select list and a submit button. I want to pass both the select list value and a separate javascript variable to the same php file simultaneously on the click of the submit button. Can anyone point me in the right direction?
0
Comment
Question by:geoffcoles
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 12

Expert Comment

by:Barry Jones
ID: 24858572
Use the form onsubmit handler to set a hidden form input elements' value to that of the javascript variable before posting.  See code below (untested):
<script type="text/javascript">
  fnPostForm = function(){
    var frm = document.getElementById("frmThis");
    var js_var = document.getElementById("js_variable");
    js_var.value = my_javascript_variable;
    frm.submit();
    return true;
  }
</script>
 
<form id="frmThis" action="script.php" method="post" onsubmit="return fnPostForm();">
  <fieldset>
    <input id="js_variable" name="js_variable" value="" />
    ...
      Other form fields, selects etc
    ...
    <input type="submit" id="cmdSubmit" value="Submit" />
  </fieldset>
</form>

Open in new window

0
 
LVL 12

Expert Comment

by:Barry Jones
ID: 24858584
Actually you dont need the line frm.submit() in the above code..
0
 
LVL 12

Accepted Solution

by:
Barry Jones earned 1000 total points
ID: 24858590

<script type="text/javascript">
  fnPostForm = function(){
    document.getElementById("js_variable").value = my_javascript_variable;
    return true;
  }
</script>
 
<form id="frmThis" action="script.php" method="post" onsubmit="return fnPostForm();">
  <fieldset>
    <input id="js_variable" name="js_variable" value="" />
    ...
      Other form fields, selects etc
    ...
    <input type="submit" id="cmdSubmit" value="Submit" />
  </fieldset>
</form>

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 14

Expert Comment

by:profya
ID: 24858902
Try this simple code:
</script>
<form mathod="post" action="target.php" name="form1" onsubmit="return function1();">
<select name="select1">
<option value="1">1</option>
<option value="2">2</option>
</select>
<input type="hidden" name="myvar" >
<input type="submit" value="submit">
</form>

Open in new window

0
 
LVL 14

Expert Comment

by:profya
ID: 24858911
Sorry,
<script type="text/javascript">
function function1()
{
	document.forms[0].elements['myvar'].value="new value";
	return true;
}
</script>
<form mathod="post" action="target.php" name="form1" onsubmit="return function1();">
<select name="select1">
<option value="1">1</option>
<option value="2">2</option>
</select>
<input type="hidden" name="myvar" >
<input type="submit" value="submit">
</form>

Open in new window

0
 

Author Closing Comment

by:geoffcoles
ID: 31603682
Thanks so much for this solution!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question