Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 218
  • Last Modified:

post javascript variable and selectlist value simultaneously to php file

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
geoffcoles
Asked:
geoffcoles
  • 3
  • 2
1 Solution
 
Barry JonesCommented:
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
 
Barry JonesCommented:
Actually you dont need the line frm.submit() in the above code..
0
 
Barry JonesCommented:

<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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
profyaCommented:
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
 
profyaCommented:
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
 
geoffcolesAuthor Commented:
Thanks so much for this solution!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now