Javascript form redirect, not redirecting

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
davidtube
Forum Commoner
Posts: 79
Joined: Sun Mar 25, 2007 8:42 pm

Javascript form redirect, not redirecting

Post 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>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Give this a try

Code: Select all

document.getElementById("articleform").action
davidtube
Forum Commoner
Posts: 79
Joined: Sun Mar 25, 2007 8:42 pm

Post by davidtube »

Thanks but I tried

Code: Select all

document.getElementById("articleform").action = "editingarticle.php";
and it didn't work.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post 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> 
There are 10 types of people in this world, those who understand binary and those who don't
davidtube
Forum Commoner
Posts: 79
Joined: Sun Mar 25, 2007 8:42 pm

Post 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.
davidtube
Forum Commoner
Posts: 79
Joined: Sun Mar 25, 2007 8:42 pm

Post 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" />
				&nbsp;</td>
			</tr>
			<tr>
				<td style="width: 109px">Article Body:</td>
				<td>&nbsp;</td>
			</tr>
			<tr>
				<td colspan="2">
					<textarea name="TextArea1" cols="20" rows="2"></textarea>
				&nbsp;</td>
			</tr>
			<tr>
				<td style="width: 109px">&nbsp;</td>
				<td>
					<input name="Button1" type="button" value="Save" onClick='return requiredinfo();' /></form>
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

You forgot to call document.articleform.submit() ...
There are 10 types of people in this world, those who understand binary and those who don't
davidtube
Forum Commoner
Posts: 79
Joined: Sun Mar 25, 2007 8:42 pm

Post by davidtube »

That still doesn't take you to the new page though. Thanks though.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post 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" />
            &nbsp;</td>
         </tr>
         <tr>
            <td style="width: 109px">Article Body:</td>
            <td>&nbsp;</td>
         </tr>
         <tr>
            <td colspan="2">
               <textarea name="TextArea1" cols="20" rows="2"></textarea>
            &nbsp;</td>
         </tr>
         <tr>
            <td style="width: 109px">&nbsp;</td>
            <td>
               <input name="Button1" type="button" value="Save" onClick='requiredinfo();' /></form>
Tested with: FF, Opera, IE
There are 10 types of people in this world, those who understand binary and those who don't
davidtube
Forum Commoner
Posts: 79
Joined: Sun Mar 25, 2007 8:42 pm

Post by davidtube »

Oh that's great. Thanks :D
Post Reply