IqbalBary
IqbalBary

Reputation: 1126

Send value with Onchange and javascript function

I'm new at JavaScript and I need some help to submit my form when anyone select anything from the dropdown list. Here is the sample code :

<form action="" method="post">
  <select name="car" onChange="this.form.submit()">
    <option value="car1">car1</option>
    <option value="car1">car1</option>
    <option value="car1">car1</option>
  </select>
  <!-- Some other code -->
  <input type="submit" name="sentvalue">
</form>

This is my form and when anyone selects the dropdown, the form automatic submit but I also need the sentvalue when automatic submit the form. So can anyone help me to capture the sentvalue when anyone select the dropdown.

Upvotes: 1

Views: 846

Answers (4)

imkrisna
imkrisna

Reputation: 853

Just give the <input> tag a name

<input name="submitbtn" type="submit" value="sentvalue" /> 

then you can get it by $_POST['submitbtn'] in case of PHP which value is "sentValue"

Upvotes: 1

Aslam Khan
Aslam Khan

Reputation: 81

You will have to create a hidden input element

<input type='hidden' id='sentvalue' value='' />

and call a function on submit instead of directly submitting Use this in the function to set the values and submit

var e = document.getElementByName("car");
document.getElementById("sentvalue").value = e.options[e.selectedIndex].value;
this.form.submit();

Upvotes: 0

zzlalani
zzlalani

Reputation: 24354

If you want to send any other data with the form submit you can send it in hidden inputs

<input type='hidden' name='data1' value='Test1' />
<input type='hidden' name='data2' value='Test2' />
//etc

And if you want the value of submit button with the form just set the value attribute into your code for submit button every this else seems fine,

<input type="submit" name="sentvalue" value="Test1" />

Hope this answers your question

Upvotes: 1

Marko Kudjerski
Marko Kudjerski

Reputation: 348

It sounds like you want the value of the input field (sentvalue) to be submitted as well. But how do you guarantee that the value was specified by the user? Is this supposed to be a hidden input field?

Either way your input tag is incomplete. This sounds better:

<input type="text" name="sentvalue"></input>

Also, when you submit, the value of this field (sentvalue) will be passed in too. As long as your tags are right you don't need to worry.

Upvotes: 0

Related Questions