Log in

View Full Version : Onclick check checkbox then do action



motormichael12
12-02-2006, 04:17 AM
I have the code:


<?php
$thefile = 'shouts.txt';
$dataf = 'shouts.txt';
$url = $_POST['url'];
$message = $_POST['message'];

function deleteLine($file, $lineNum) {
$c = file($file);
$c[$lineNum] = '';
$c = implode('', $c);
$f = fopen($file, 'w');
fwrite($f, $c);
fclose($f);
}

if(isset($_POST['Submit']))
$file = fopen("$dataf","a");
$write = fwrite($file,"<a href=$url>$message</a> &middot;&middot; ($url)
");
fclose($file);


if(isset($_POST['lines']) && isset($_POST['del']))
for($i = 0; $i < count($_POST['lines']); ++$i)
deleteLine($thefile, $_POST['lines'][$i]);

$c = file('shouts.txt');
?>
<form action="<?php echo($PHP_SELF); ?>" method="post">
<?php for($i = 0; $i < count($c); ++$i) { ?>
<label style="display: block;">
<input type="checkbox" name="lines[]" value="<?php echo($i); ?>"><input type="submit" name="del" value="X">
<?php echo($c[$i]); ?>
</label>
<?php } ?>
#1<input name="message" size= "30" type="text" id="message">
#2<input name="url" size="30" type="text" id="url">
<input type="submit" name="Submit" value="Submit"><input type="reset" name="Reset" value="Reset">
</form>


I need to make it so that when you click on the button named "del" it will do the action of deleting the line... Currently it has to be set that the box has to be checked before it deletes, how do I make it so it doesn't need to be checked, just click the button next to it and it deletes the line?

thetestingsite
12-02-2006, 05:26 PM
you could probably make up the submit button just a button, then do onclick="window.location='?act=deleteline&linenum=<?php echo ($i); ?>';">

Then in the php file itself, add $act = $_REQUEST["act"]; and make an if statement that goes like so - if ($act == "deleteline") { deleteline($linenum); }
or something to that effect.
Hope this kinda helps.