0

I think javascript could be handy here.
To every button assingning an "onclick" event, which overrides the forms "action" field, adding an extra argument, the rows ID.
So, after submitting the form the URL would be, for example "index.php?id=3".
All what remains is to pass the $_GET variable and delete the row.

Edited by amd_k8: n/a

0

I think javascript could be handy here.
To every button assingning an "onclick" event, which overrides the forms "action" field, adding an extra argument, the rows ID.
So, after submitting the form the URL would be, for example "index.php?id=3".
All what remains is to pass the $_GET variable and delete the row.

Well, I don't know anything about javascrpit, but I see what you're saying. Could I do this with php? like this

<?php
$site=''.$row['SiteNumber'].'';
?>
<a href="memberpage.php?delete=<?php print $site ?>">Delete</a>

Then on down, something like

switch ($_GET['delete']){
case print $site:
	{
		
		mysql_query("DELETE FROM UserSites WHERE SiteNumber=".mysql_real_escape_string($_POST[print $site]));
		echo '<strong>Site Deleted!</strong><br>'."\n";
		echo '<a href="'.$_SERVER['PHP_SELF'].'"'."\n";
	}
}

Edited by sfrider0: n/a

0

Here is a very simple example to dynamically change forms "action" using javascript. Let's assume we have 4 rows, like in your app, so i created 4 buttons.
Every button when clicked, first will change the forms "action" field, the submits it.
I think that tuning this example into your app wouldn't take much time...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>

<head>
	<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
	<meta name="author" content="mazuki" />

	<title>Untitled 3</title>
</head>

<script type="text/javascript">
var changeId = function(id)
{
	document.f1.action += '?id=' + id; 
}
</script>

<body>

<?php

if($_SERVER['REQUEST_METHOD'] == 'POST')
{
	if(isset($_GET['id']))
	{
		$r = (int)$_GET['id'];
		echo 'Row to delete: '. $r;
	}
}

?>

<form name="f1" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="submit" value="Delete" onclick="changeId(1)" /><br />
<input type="submit" value="Delete" onclick="changeId(2)" /><br />
<input type="submit" value="Delete" onclick="changeId(3)" /><br />
<input type="submit" value="Delete" onclick="changeId(4)" />
</form>

</body>
</html>

Edited by amd_k8: n/a

0

Here is a very simple example to dynamically change forms "action" using javascript. Let's assume we have 4 rows, like in your app, so i created 4 buttons.
Every button when clicked, first will change the forms "action" field, the submits it.
I think that tuning this example into your app wouldn't take much time...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>

<head>
	<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
	<meta name="author" content="mazuki" />

	<title>Untitled 3</title>
</head>

<script type="text/javascript">
var changeId = function(id)
{
	document.f1.action += '?id=' + id; 
}
</script>

<body>

<?php

if($_SERVER['REQUEST_METHOD'] == 'POST')
{
	if(isset($_GET['id']))
	{
		$r = (int)$_GET['id'];
		echo 'Row to delete: '. $r;
	}
}

?>

<form name="f1" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="submit" value="Delete" onclick="changeId(1)" /><br />
<input type="submit" value="Delete" onclick="changeId(2)" /><br />
<input type="submit" value="Delete" onclick="changeId(3)" /><br />
<input type="submit" value="Delete" onclick="changeId(4)" />
</form>

</body>
</html>

But why? Some users (me included) have JavaScript disabled by default. Using it for a key function might not be the best idea. And like I said, the code should work. It should delete. The only thing I can think would be that the your MySQL is incorrect. What does the table look like that your are deleting the row from...

Also, just wondering: It sounds like you've made a couple changes to the code (beyond what we have suggested)...can you copy and paste the exact code that you are running?

Edited by FlashCreations: n/a

0

Okay, if you some people have js disabled, the single way i see, is to create a separate form for each row.

0

Can you use php if you want to put a delete button for row? Wouldn't SiteNumber get replaced by the next row?

0

Nope, it wouldn't. Because each form will have it's own sitenumber hidden field, which will be sent by clicking the button.
We create not a single form for all rows, but a form for each row.
But i totally dislike this approach, because as said lately, some people have js disabled, and by my opinion thats wrong.

0

And by your opinion that's wrong? Site Usability 101 says "Don't use JavaScript when it's not necessary." As I said before, using multiple forms., hidden text fields (for the Site Number), and submit buttons to send the form (delete the site).

0

This "usability" is a very hazy stuff. I think that there is no need to enumerate all of the web aspects achieved with javascript...

0

So then instead of worrying about JavaScript support, you can simply rely on the solution that will work regardless of whether the user has JavaScript enabled:

<?php
error_reporting(E_ALL); //So you can find any errors (If you don't have them enabled)
mysql_connect(" ", "", "") or die(mysql_error()); //Replace with login credentials
mysql_select_db("TACusers") or die(mysql_error());
$result = mysql_query("SELECT Site, SiteNumber, URL, Description, Type, Rating FROM UserSites WHERE Username='".mysql_real_escape_string($username)."' ORDER by Site");

if(!isset($cmd))
{
	//Print all of the users sites
	while($row = mysql_fetch_assoc($result))
	{
		echo '			<a href="'.$row["URL"].'">'.$row["Site"].'</a> '.
			 '			'.$row['Type'].' '.
			 '			'.$row['Description'].' '.
			 '			<img src="'.$row['Rating'].'" alt="'.$row['Description'].'" /> '.
			 '				<form action="'.$_SERVER['PHP_SELF'].'" method="post">'."\n".
			 '					<input type="hidden" name="SiteNumber" value="'.$row["SiteNumber"].'">'."\n";
			 '					<input type="submit" name="submit" value="Delete">'."\n".
			 '				</form><br>'."\n";
	}
}

if(isset($_POST['submit']) && $_POST['submit']=="Delete")
{
	mysql_query("DELETE FROM UserSites WHERE SiteNumber=".mysql_real_escape_string($_POST['SiteNumber']));
	echo '<strong>Site Deleted!</strong><br>'."\n";
	echo '<a href="'.$_SERVER['PHP_SELF'].'">Go Back</a>'."\n";
}
?>

And so the form displays correctly, add this CSS to your page:

form { display: inline; }
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.