JavaScript and client side scripting.
Moderator: General Moderators
davidtube
Forum Commoner
Posts: 79 Joined: Sun Mar 25, 2007 8:42 pm
Post
by davidtube » Fri Nov 23, 2007 10:31 am
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>
davidtube
Forum Commoner
Posts: 79 Joined: Sun Mar 25, 2007 8:42 pm
Post
by davidtube » Fri Nov 23, 2007 1:09 pm
Thanks but I tried
Code: Select all
document.getElementById("articleform").action = "editingarticle.php";
and it didn't work.
VladSun
DevNet Master
Posts: 4313 Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria
Post
by VladSun » Fri Nov 23, 2007 9:09 pm
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 » Mon Nov 26, 2007 4:42 pm
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 » Thu Nov 29, 2007 7:35 am
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>
VladSun
DevNet Master
Posts: 4313 Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria
Post
by VladSun » Thu Nov 29, 2007 7:45 am
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 » Thu Nov 29, 2007 10:09 am
That still doesn't take you to the new page though. Thanks though.
VladSun
DevNet Master
Posts: 4313 Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria
Post
by VladSun » Thu Nov 29, 2007 10:16 am
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
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 » Thu Nov 29, 2007 10:32 am
Oh that's great. Thanks