Jump to content

Form Variable not Posting Correctly


jsparrow

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.