Forum Moderators: open

Message Too Old, No Replies

Set form action onClick according to which radio button is selected

         

Br3nn4n

5:14 am on Jan 23, 2008 (gmt 0)

10+ Year Member



I'm trying to make a simple admin page for my comments system. It runs off mysql; when a user submits a comment it's entered into the database but isn't shown until it's approved...hence the need for the admin page. :)

On the page I want two radio buttons: approve and delete. Then depending on which is 'checked' the form is either submitted to 'comments.php?app=1' OR 'comments.php?del=1'.

Note: If I visit either of those two given addresses, PHP does it's thing. That part of my code works great (uses POST data as well, to specify which comment to approve or delete, just to clarify).

My javascript isn't changing the form action though! I've looked over my code over and over and I an clueless as to why it's not doing it. Here's some source:

<script type="text/javascript" language="JavaScript">
function deleteapprove() {
if(document.forms[0].radio[0].checked == true) {
document.forms[0].action = 'comments.php?app=1';
return false;
}
if(document.forms[0].radio[1].checked == true) {
document.forms[0].action = 'comments.php?del=1';
return false;
}
}
</script>
<?php

$query1 = mysql_query("SELECT * FROM comments WHERE `is_approved`='0'");

while ($result = mysql_fetch_array($query1)) {
for ($i = 0; $i < mysql_num_rows($query1);) {
echo "<div style=\"border:2px solid black;padding:2px 2px 2px 0px;\">";
echo "{$result['page']} - Article {$result['art_id']}";
echo "<form name=\"appordel\" method=\"POST\" onSubmit=\"return deleteapprove()\">";
echo "<b>$result[name]&nbsp;¦&nbsp;</b></a>";
echo "<small><i>$result[date]</i>&nbsp;¦&nbsp;";
echo "$result[comments]</small>&nbsp;¦&nbsp;<br /><hr width='25%' align='left'><br />";
echo "<input type=\"hidden\" name=\"del\" value=\"$result[com_id]\">";
echo "<input type=\"hidden\" name=\"app\" value=\"$result[com_id]\">";
echo "Approve <input type=\"radio\" name=\"option\">";
echo "Delete <input type=\"radio\" name=\"option\">";
echo "<input type=\"submit\" value=\"<-- Do it!\">";
echo "</form>";
/*echo "<img src=\"images/articles/$result[photo].jpg\" style=\"float:right;\">";*/
echo "<br /><br />";
echo "</div>";
break;
}}

I'm really trying to get this working...any ideas?

Thanks in advance!

vincevincevince

12:30 pm on Jan 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



value=\"<-- Do it!\">"; --> value=\"&lt;-- Do it!\">";

rocknbil

6:03 pm on Jan 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard Br3nn4n,

depending on which is 'checked' the form is either submitted to 'comments.php?app=1' OR 'comments.php?del=1'

You shouldn't even have to do this, you should be able to just submit to comments.php and parse out the value of the selected radio button. Change the name of the radio button to something like

<input type="radio" name="comment_action" value="approve" checked> Approve
<input type="radio" name="comment_action" value="delete"> Delete

Then in your PHP script, you just act on whatever the value of comment_action is.

Br3nn4n

2:13 am on Jan 25, 2008 (gmt 0)

10+ Year Member



Awesome, both of you! Works perfectly! :D