Solved

post javascript variable and selectlist value simultaneously to php file

Posted on 2009-07-15
6
203 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
  • 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 250 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now