Page 1 of 1
Javascript form redirect, not redirecting
Posted: Fri Nov 23, 2007 10:31 am
by davidtube
Could someone please explain to me why the following code doesn't take the browser to "editingarticle.php" when the user clicks the button? "Test" displays as it should so I'm pretty sure it's to do with the line below it.
Code: Select all
<script type="text/javascript">
<!--
function requiredinfo()
{
if (document.articleform.title.value.length < 1) {
alert("Please select or enter a title");
return false;
}
else{
alert("Test);
document.articleform.action = "editingarticle.php";
}
}
//-->
</script>
Code: Select all
<form name="articleform" id='articleform' method="post">
<input name="title" id="title" type="text" value="Create a Title" />
<input name="Button1" type="button" value="Save" onClick='return requiredinfo();' />
</form>
Posted: Fri Nov 23, 2007 12:08 pm
by John Cartwright
Give this a try
Code: Select all
document.getElementById("articleform").action
Posted: Fri Nov 23, 2007 1:09 pm
by davidtube
Thanks but I tried
Code: Select all
document.getElementById("articleform").action = "editingarticle.php";
and it didn't work.
Posted: Fri Nov 23, 2007 9:09 pm
by VladSun
Do you test it on Opera? Because I can hardly remember that Opera has some difficulties with dynamic changes in "action" form paramter.
Anyway - here is my code:
Code: Select all
<script>
function setAction(elem)
{
elem.parentNode.action = elem.alt;
return true;
}
</script>
<form action="">
<input type="submit" value="Go to dir.bg" alt="http://dir.bg" onclick="setAction(this)">
<input type="submit" value="Go to mail.bg" alt="http://mail.bg" onclick="setAction(this)">
</form>
Posted: Mon Nov 26, 2007 4:42 pm
by davidtube
No I wasn';t using opera. I've still not fixed it but I haven't had much time on it so I'm going to start again from scratch tomorrow. Thanks for your suggestions.
Posted: Thu Nov 29, 2007 7:35 am
by davidtube
OK. Here's my complete code. Can anyone see what's wrong with it? It will display the "hello" alert but won't redirect.
Code: Select all
<script type="text/javascript">
<!--
function requiredinfo()
{
if (document.articleform.title.value.length < 1) {
alert("Please select or enter a title");
return false;
}
else{
alert("hello");
document.articleform.action = "http://localhost/3/joining.php";
}
}
//-->
</script>
Code: Select all
<form name="articleform" id='articleform' method="post">
<table style="width: 100%">
<tr>
<td style="width: 109px">Article Title:</td>
<td>
<select name="Select1">
<option>Select a Title</option>
<option>Details</option>
</select> Or <input name="title" id="title" type="text" value="Create a Title" />
</td>
</tr>
<tr>
<td style="width: 109px">Article Body:</td>
<td> </td>
</tr>
<tr>
<td colspan="2">
<textarea name="TextArea1" cols="20" rows="2"></textarea>
</td>
</tr>
<tr>
<td style="width: 109px"> </td>
<td>
<input name="Button1" type="button" value="Save" onClick='return requiredinfo();' /></form>
Posted: Thu Nov 29, 2007 7:45 am
by VladSun
You forgot to call document.articleform.submit() ...
Posted: Thu Nov 29, 2007 10:09 am
by davidtube
That still doesn't take you to the new page though. Thanks though.
Posted: Thu Nov 29, 2007 10:16 am
by VladSun
Code: Select all
<script type="text/javascript">
<!--
function requiredinfo()
{
if (document.getElementById('id_title').value.length < 1)
{
alert("Please select or enter a title");
}
else
{
alert("hello");
document.getElementById('id_articleform').action = "http://google.com";
document.getElementById('id_articleform').submit();
}
}
//-->
</script>
HTML:
<form name="articleform" id="id_articleform" method="post">
<table style="width: 100%">
<tr>
<td style="width: 109px">Article Title:</td>
<td>
<select name="Select1">
<option>Select a Title</option>
<option>Details</option>
</select> Or <input name="title" id="id_title" type="text" value="Create a Title" />
</td>
</tr>
<tr>
<td style="width: 109px">Article Body:</td>
<td> </td>
</tr>
<tr>
<td colspan="2">
<textarea name="TextArea1" cols="20" rows="2"></textarea>
</td>
</tr>
<tr>
<td style="width: 109px"> </td>
<td>
<input name="Button1" type="button" value="Save" onClick='requiredinfo();' /></form>
Tested with: FF, Opera, IE
Posted: Thu Nov 29, 2007 10:32 am
by davidtube
Oh that's great. Thanks
