PDA

View Full Version : using one form for two scripts



thetrend
11-02-2008, 05:18 AM
this may have a totally simple solution, but I'm a newb when it comes to JavaScript (I'm more PHP-oriented).

here are my codes, as you can tell I did not write them, seeing as I have very little knowledge of javascripting.

the javascript:

<script type="text/javascript">
//<![CDATA[
<!--
/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Nannette Thacker | http://www.shiningstar.net */
var checkflag = "false";
function check(field) {
if (checkflag == "false") {
for (i = 0; i < field.length; i++) {
field[i].checked = true;
}
checkflag = "true";
return "Uncheck All";
} else {
for (i = 0; i < field.length; i++) {
field[i].checked = false;
}
checkflag = "false";
return "Check All";
}
}

/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Robin Jones :: http://www.robinjones1.freeuk.com */

function jumpBox(list) {
location.href = list.options[list.selectedIndex].value
}

-->
//]]>
</script>

the html:
First, the checkboxes:

<form name="myform" action="" method="post">
<table>
<tr>
<td><strong>Make a selection</strong>
<br />
<input type="checkbox" name="list" value="1" />Java
<br />
<input type="checkbox" name="list" value="2" />JavaScript
<br />
<input type="checkbox" name="list" value="3" />ASP
<br />
<input type="checkbox" name="list" value="4" />HTML
<br />
<input type="checkbox" name="list" value="5" />SQL
<br />
<br />
<input type="button" value="Check All" onclick="this.value=check(this.form.list)" /></td>
</tr>
</table>
</form>
<br />

and the dropdown:

<form>
<select>
<option selected="selected">
Select a page
</option>

<option value="demo1.html">
Page One
</option>

<option value="demo2.html">
Page Two
</option>

<option value="demo3.html">
Page Three
</option>
</select> <input type="button" value="Go" onclick="jumpBox(this.form.elements[0])" />
</form>

I'd like to consolidate them into one form, and on a sidenote, I'd like to have innerhtml $number of items selected; I know how it works, but like I said, since I'm a JS newb, I don't know how to write it. xP

one reason why I'd like to consolidate the forms is that I'm writing a private message system in php and I was going to use the option values as the form action url.

this is how I was going to format it:

<form> <table width="90%" align="center" border="0" style="border: 0">
<tr>
<th style="width: 50%; text-align: left">&nbsp;&nbsp;Letter</th>
<th style="width: 20%">Date</th>
<th style="width: 18%">Status</th>
<th style="width: 12%">Select</th>
</tr>
<?php
while($mail = mysql_fetch_object($getmail)) {
$status = $mail->status;
if($status == '0') {
$title = "<b>".$mail->title."</b>";
$status = "Unopened";
} elseif($status == '1') {
$title = "<em>".$mail->title."</em>";
$status = "Read";
} elseif($status == '2') {
$title = $mail->title;
$status = "Replied";
}
?>
<tr>
<td style="text-align: left">
&nbsp;&nbsp;<a href="/mail/view.php?id=<?php echo $mail->id ?>"><?php echo $title ?></a>
from <a href="/profile.php?user=<?php echo $mail->sender ?>"><?php echo $mail->sender ?></a>
</td>
<td><?php echo $mail->sentdate ?></td>
<td><?php echo $status ?></td>
<td><input type="checkbox" name="check" id="letter[<?php echo $mail->id ?>]" value="<?php echo $mail->id ?>" /></td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" style="text-align: right">
<span id="msg"></span><span id="link" title="Only works if there's more than one!"><a href="javascript:selectAll()">Select all</a></span>
<select>
<option value="">Select a page</option>
<option value="?action=archive">Archive</option>
<option value="?action=delete">Delete</option>
</select>
<input style="margin: 0; padding: 0" type="button" value="Go" onclick="jumpBox(this.form.elements[0])" /></form>
</td>
</tr>
</table>

or at least, that's how my old code was, but it confused me (the php is mine, the javascript bits are all from random resources, but I'd rather start over with help. =]

merci to anyone who can help ^_^

jscheuer1
11-02-2008, 01:42 PM
I really don't follow PHP too well, so haven't tried to read yours, so I may be missing something. However, I don't see a conflict between your two javascript functions visa vis their both being used in one form as opposed to two forms.

The only thing that's potentially a problem is:


this.form.elements[0]

as the select may no longer be the first (0) element in the form. If this happens, simply use its correct number.

thetrend
11-02-2008, 06:40 PM
well I was wondering how to put it all into one form, so I can just put it together and adjust the element number? =D

jscheuer1
11-02-2008, 07:29 PM
Yep, that's what I said. Alternatively you could access the desired select element via an id or name, but if you control the markup (and the user cannot change it via actions he/she takes) and you don't want the select's value passed when the form is submitted, using its number is the most cross browser choice available.

thetrend
11-02-2008, 07:53 PM
ah, thanks so much =]