jsparrow Posted January 21, 2011 Share Posted January 21, 2011 I have a php index page that includes a form page with a delete button for that row. When I hit the delete button nothing happens. I am not sure if the variable is not passing or if the index.php page is not processing it correctly. Here is the index page that includes the form page: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Welcome Page Test</title> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> </head> <body> <?php if (get_magic_quotes_gpc()) { function stripslashes_deep($value) { $value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value); return $value; } $_POST = array_map('stripslashes_deep', $_POST); $_GET = array_map('stripslashes_deep', $_GET); $_COOKIE = array_map('stripslashes_deep', $_COOKIE); $_REQUEST = array_map('stripslashes_deep', $_REQUEST); } if (isset($_GET['addjoke'])) { include 'form.html.php'; exit(); } $link = mysqli_connect('localhost', 'root', 'password'); if (!$link) { $output = 'Unable to connect to the database server.'; include 'output.html.php'; exit(); } if (!mysqli_set_charset($link, 'utf8')) { $output = 'Unable to set database connection encoding.'; include 'output.html.php'; exit(); } if (!mysqli_select_db($link, 'database')) { $output = 'Unable to locate the database.' . mysqli_error($link); include 'output.html.php'; exit(); } if (isset($_POST['joketext'])) { $joketext=mysqli_real_escape_string($link,$_POST['joketext']); $sql = "INSERT INTO jokes2 (joketext, jokedate) VALUES ('$joketext', CURDATE() )"; if (!mysqli_query($link,$sql)) { $error = 'Error adding submitted joke: ' . mysqli_error($link); include 'error.html.php'; exit(); } header('Location: .'); exit(); } if (isset ($_GET['deletejoke'])) { $id= mysqli_real_escape_string($link,$_POST['id']); $sql = "DELETE FROM jokes2 WHERE id='$id'"; if(!mysqli_query($link,$sql)) { $error= 'Error deleting actor: ' . mysqli_error($link); include 'error.html.php'; exit(); } header('Location: .'); exit(); } $result = mysqli_query($link, 'SELECT jokes2.id, joketext, name, email FROM jokes2 INNER JOIN author ON authorid = author.id'); if (!$result) { $error = 'Error retrieving data' . mysqli_error($link); include 'error.html.php'; exit(); } while ($row = mysqli_fetch_array($result)) { $jokes[] = array('id'=>$row['id'], 'joketext'=>$row['joketext'], 'name'=>$row['name'], 'email'=>$row['email']); } include 'jokes.html.php'; ?> </body> </html> The $_GET array should be getting the variable id passed when the Delete button is pressed on the form, but nothing is happening when I press the button. Here is the form: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Welcome Page Test</title> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> </head> <body> <p> <a href="?addjoke">Add Joke </a> </p> <p> Here are all the jokes: </p> <form action="?deletejoke" method="post"> <?php foreach($jokes as $joke): ?> <p> <?php echo htmlspecialchars($joke['id'], ENT_QUOTES, 'UTF-8'), htmlspecialchars($joke['joketext'], ENT_QUOTES, 'UTF-8'), htmlspecialchars($joke['name'], ENT_QUOTES, 'UTF-8'), htmlspecialchars($joke['email'], ENT_QUOTES, 'UTF-8'); ?> <input type="hidden" name="id" value="<?php echo $joke['id']; ?>" /> <input type="submit" value="Delete"/> </p> </form> <?php endforeach; ?> </p> </body> </html> All of my other variables seem to be passing just fine. Any ideas why this one is a problem? Quote Link to comment https://forums.phpfreaks.com/topic/225261-form-variable-not-posting-correctly/ Share on other sites More sharing options...
requinix Posted January 21, 2011 Share Posted January 21, 2011 Are you sure it's not just deleting the last joke in the table each time? Quote Link to comment https://forums.phpfreaks.com/topic/225261-form-variable-not-posting-correctly/#findComment-1163350 Share on other sites More sharing options...
jsparrow Posted January 21, 2011 Author Share Posted January 21, 2011 I checked that and it's not deleting anything. I'm not sure where this is falling down. Quote Link to comment https://forums.phpfreaks.com/topic/225261-form-variable-not-posting-correctly/#findComment-1163357 Share on other sites More sharing options...
jsparrow Posted January 24, 2011 Author Share Posted January 24, 2011 Anyone have any advice on this. I've been trying to work it out over the weekend and still not working. Quote Link to comment https://forums.phpfreaks.com/topic/225261-form-variable-not-posting-correctly/#findComment-1164418 Share on other sites More sharing options...
PFMaBiSmAd Posted January 24, 2011 Share Posted January 24, 2011 Your delete form has got one opening <form> tag (before the start of the foreach(){} loop), but you are closing that </form> inside of the foreach(){} loop, over and over, thereby making the HTML invalid. Start by making sure the 'view source' of your form is what you want. Quote Link to comment https://forums.phpfreaks.com/topic/225261-form-variable-not-posting-correctly/#findComment-1164426 Share on other sites More sharing options...
jsparrow Posted January 24, 2011 Author Share Posted January 24, 2011 Thanks PFMaBiSmAd. After moving the form start to after the <?php foreach statement and containing the entire form within the foreach statement it worked. Quote Link to comment https://forums.phpfreaks.com/topic/225261-form-variable-not-posting-correctly/#findComment-1164486 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.