2

I am using Javascript to pass form variables to the PHP page sprcial_action.php but it is not redirecting to that page. My code is given below, please tell me where is the problem?

<script>
function formSubmit(index)
{
 var image = document.getElementById("hdnimage" + index);
 var email = document.getElementById("email" + index);
 window.location.href = "sprcial_action.php?name=" + image; 
}
</script>
<form enctype="multipart/form-data" id="form1" name="form1" method="get">
<div class="offers">
 <div class="offer-banner">
 <img src="/images/specialoffer/offer-1.png" />
 </div>
 <div class="mobile">
 <input type="text" value="Mobile" name="mobile" />
 </div>
 <div class="or-button"></div>
 <div class="email">
 <input type="text" value="Email Address" name="email" id="email0" />
 </div>
 <input type="hidden" name="hdnimage0" id="hdnimage0" value="/images/specialoffer/offer-1.png" />
 <div class="redem-copon">
 <!--<a href="#"><img src="/images/specialoffer/copun.png" /></a>-->
 <input type="image" src="/images/specialoffer/copun.png" onclick="formSubmit(0)" />
 </div>
</div>
</form>
irrelephant
4,1112 gold badges28 silver badges41 bronze badges
asked Nov 29, 2012 at 6:13
1
  • my page name is sprcial_action.php not special_action.php Commented Nov 29, 2012 at 6:16

4 Answers 4

2

window.location.href needs full path starting with http://. and add .value to image object

you should be writing as

window.location.href = "http://mydomain.com/sprcial_action.php?name=" + image.value;
answered Nov 29, 2012 at 6:16
Sign up to request clarification or add additional context in comments.

1 Comment

+1. Also not canceling default behavior may be the second half of the problem (after this navigation).
1

Your image variable refers to the input object, not its value. To get its value, add .value:

window.location.href = "sprcial_action.php?name=" + image.value;
// here -------------------------------------------------^

But note that you're getting the email field but not then doing anything with it, and setting the href does not submit a form, it just takes you to the given page.

If you want to change the location the form submits to, you have to set the action property on the form object (and not change location.href):

var form = document.getElementById("form1");
form.action = image.value;

I'm not 100% certain you can change that during a form submit reliably across browsers, you'll need to test on your target browsers.

answered Nov 29, 2012 at 6:17

Comments

1

If 'sprcial_action.php' page is in the same application then please refer to the above answer(@Rab'a Answer), you have to use image.value instead of image( As it will be treated as a object). You don't need to give the full path. But if this page is outside the current application then you have to specify the full address.

window.location.href = "http://imagesheaven.com/sprcial_action.php?name=" + image.value;

Try using document.location.href also. Thanks!!

answered Nov 29, 2012 at 6:30

Comments

1

have the answer:

 <script>
function formSubmit(index)
{
 var image = document.getElementById("hdnimage" + index);
 var email = document.getElementById("email" + index);
var data=image.value;
 window.location.href = "http://yourservername/sprcial_action.php?name=" + data; 
}
</script>
<form enctype="multipart/form-data" id="form1" name="form1" method="get">
<div class="offers">
 <div class="offer-banner">
 <img src="/images/specialoffer/offer-1.png" />
 </div>
 <div class="mobile">
 <input type="text" value="Mobile" name="mobile" />
 </div>
 <div class="or-button"></div>
 <div class="email">
 <input type="text" value="Email Address" name="email" id="email0" />
 </div>
 <input type="hidden" name="hdnimage0" id="hdnimage0" value="/images/specialoffer/offer-1.png" />
 <div class="redem-copon">
 <!--<a href="#"><img src="/images/specialoffer/copun.png" /></a>-->
 <input type="image" src="/images/specialoffer/copun.png" onclick="formSubmit(0)" />
 </div>
</div>
</form>
answered Nov 29, 2012 at 6:49

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.