I am injecting javascript into a PHP website to avoid a pop-up, to submit a form automatically. Also, there is an issue with jquery so I am using plain javascript.
This is the form on the page:
<form action='http://mywebsite.com/index.php?&act=MYFUNCTION&CODE=01&CookieDate=1' name='subscribe_check' method='POST'>
<input type='hidden' name='value1' value='dynamicallygenerated'>
<input type='hidden' name='Value2' value='BlogSection'>
<input type='hidden' name='Value3' value='BlogName'>
<select class='forminput' name='sub_id' onchange='this.form.submit()'>
<option value='------------' selected='selected'>To read this article, you must choose a subscription level</option>
<option value='1'>Subscribe to daily updates</option>
<option value='2'>Subscribe to promotional emails</option>
<option value='3'>No thanks, I'm not interested in being healthy</option>
</select>
</form>
This is my javascript:
// unselect any selected item on the SELECT
if( document.getElementsByName('subscribe_check').selectedIndex )
document.getElementsByName('subscribe_check')[document.getElementsByName('subscribe_check').selectedIndex].selected = false;
// select select the last option
document.getElementsByName('subscribe_check')[1][3].selected = true;
// submit the form
document.forms[0].submit();
When I manually click on the form, I know that these values are set
value1: dynamicallygenerated
value2: HealthyFoodSection
value3: HealthyFoodFunBlog
But when my javascript submits the form, these values are submitted
value1: dynamicallygenerated
value2: BlogSection
value3: BlogName
I am 100% sure there is no other javascript that is firing to change the values - there can't be, the onchange calls submit() directly.
I don't understand why my javascript submitting the form doesn't change the values like clicking it manually does? If there is PHP happening, I don't understand how it would even detect that my javascript submitted the form versus me clicking submit, a submit click is a submit click, right?
BlogSectionandBlogNametoHealthyFoodSectionandHealthyFoodFunBlog?? i don't see "how" you are even able to submit those values (they'r never there).nameattribute of theinputs: for values 2 and 3 your are usingValue2andValue3instead ofvalue2andvalue3(lower case)selectedIndexis a property on the select DOM element, but the only element I see with namesubscribe_checkis the form itself. (The select has namesub_id.) Also getElementsByName returns a collection, so you can't access properties of one element directly on it anyway.