Jump to content

[SOLVED] Change Action Form - Not Working


fanfavorite

Recommended Posts

I have a form that is setup like so:

 

<form enctype="multipart/form-data" id="orderform" name="orderform" action="page1.php" method="post">
    <select name="website" onchange="showField(this.value,'havesite2','null','null')">
<option value="No"<? if ($_POST['website'] == "No") { echo ' selected="selected"'; } ?>>No</option>
<option value="Yes"<? if ($_POST['website'] == "Yes") { echo ' selected="selected"'; } ?>>Yes</option>
    </select>
    <div id="havesite2"></div>
</form>

 

Now showField updates the havesite2 div.

 

If you pick yes this is in havesite2 div:

 

<script type="text/javascript">
<!--//
document.getElementById('orderform').action = "page2.php";
//-->
</script>  

 

If you pick no this is in havesite2 div:

 

<script type="text/javascript">
<!--//
document.getElementById('orderform').action = "page1.php";
//-->
</script>  

 

I have also tried:

 

document.orderform.action = "page2.php";

 

But nothing seems to work.

 

Any ideas why?

 

Thanks,

 

-JC

 

EDIT:  Just wanted to also mention that the AJAX is actually updating the havesite2 div correctly.  I use firebug in firefox to check. 

Link to comment
Share on other sites

Thanks for the reply.  Yeah I am sure that showField is working as I insert more than just that when changed.  Instead of putting it in the div, I just rewrote the code to include it in the showField and it works now:

 

if (value == "Yes" && div1 == "havesite2") {

document.getElementById('orderform').action = "page2.php";

} else if (value == "No" && div1 == "havesite2") {

document.getElementById('orderform').action = "page1.php";

}

Link to comment
Share on other sites

Well it is working fine in Firefox, but Internet Explorer gives the following code:

 

"Object doesn't support this property or method"

 

IE7 doesn't like: 

 

document.getElementById('orderform').action = "page2.php";

and

document.getElementById('orderform').action = "page1.php";

 

Any ideas?  Could it be because this is in a separate js file? 

 

Link to comment
Share on other sites

The separate JS file doesn't matter. You aren't calling that code until after the page loads right? I just tested this in IE7 and it worked fine:

 

<html>
  <head>
    <script type="text/javascript">
window.onload = function ( ) {
  document.getElementById('orderform').action = "page2.php"; 
}
    </script>
  </head>
  <body>
    <form enctype="multipart/form-data" id="orderform" name="orderform" action="page1.php" method="post">
      <input type="submit" />
    </form>
  </body>
</html>

Link to comment
Share on other sites

Still have no idea why IE7 has the problem. Here is more of the code. I stripped a lot out for easy reading.

 

ajax.js:

 

function showField(value,div1,post1,post2,post3,post4) { 
if (value == "Yes" && div1 == "havesite2") {
document.getElementById('orderform').action = "page2.php";
} else if (value == "No" && div1 == "havesite2") {
document.getElementById('orderform').action = "page1.php";
}
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null) {
alert ("Browser does not support HTTP Request");
return;
}
var url="change.php";
url=url+"?value="+value;
url=url+"&div1="+div1;
url=url+"&post1="+post1;
url=url+"&post2="+post2;
url=url+"&post3="+post3;
url=url+"&post4="+post4;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=function () {
stateChange(div1);
};
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

 

index.php:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>TESTING</title>
<script type="text/javascript" src="js/ajax.js"></script>
</head>
<body>
<form enctype="multipart/form-data" id="orderform" name="orderform" action="page1.php" method="post">
<select name="website" onchange="showField(this.value,'havesite2','','','','')">
<option value="No"<? if ($_POST['website'] == "No") { echo ' selected="selected"'; } ?>>No</option>
<option value="Yes"<? if ($_POST['website'] == "Yes") { echo ' selected="selected"'; } ?>>Yes</option>
</select>
<script type="text/javascript">
<!--
showField('$_POST[website]','havesite2','','','','');
-->
</script>
</div>
<div id="havesite2"></div>
<input type="submit" alt="Submit" name="submit" id="submit" value=" " class="formsubmit" /></div>
</form>
</body>
</html>	

 

If I comment out the document.getElementById('orderform').action = "page2.php"; and document.getElementById('orderform').action = "page1.php"; then the script works fine other than not changing the action obviously, but no errors.

Link to comment
Share on other sites

yeah...i was just about to say...works for me (except for the AJAX part cus I don't have your code for GetXmlHttpObject()

 

...if it is the AJAX part, I recommend saving yourself the trouble and just using a JS library to do that work for you. jQuery is my favorite, but prototype and Dojo are two others that work. once you use them, you will discover how much easier javascript becomes. instead of worrying about making it work in every browser, you worry about the actual logic in your code.

Link to comment
Share on other sites

Oh man, what a headache.  I had a hidden field with the name "action", which was causing a problem with IE.  I didn't even need this hidden field anymore and just overlooked it.  After I removed this, everything is fine.  

 

Thanks for your help.  

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.