Jump to content

Help with $_post & $_Get I am a total Newbie


Gilwren

Recommended Posts

Hi Guys

 

I am a total newbie to PHP and coding for that matter and have recently done a 10 week basic course in PHP. So I should be able to answer this question but I am stuck please help. My code looks like this;

 

<?php 
  if (isset($_POST['britbatID'])) {
 header("Location:editbritbat.php?pageID=".$_POST['britbatID']."");
  }
?>
<div id="category">
            <h2>Edit a British Battalion</h2>
                    <form action="" method="post">
        <fieldset>
          <div class="selection"><label for="BritbatID">Edit British Battalion</label>
          <select name="britbatID">
                            <option value="<?php echo $row_britbatname['britbatID'];?>"><?php echo $row_britbatname['britbatname'];?></option>
                            <?php do { ?>
            <option value="<?php echo $row_britbatname['britbatID'];?>"><?php echo $row_britbatname['britbatname'];?></option>
                            <?php } while ($row_britbatname = mysql_fetch_assoc($rsbritbatname));?>">                               
          </select>
          <input name="<?php if (isset($_POST['britbatID'])) {
 header("Location:editbritbat.php");} ?>britbatID"submitBat" class="button" type="submit" value="edit this British Battalion"> </div>
                        <!-- end selection--></div>
 
However when I use the editbritbat.php file it fails to have any value for $britbatID. The relevant code in my editbritbat.php file is;
 
<?php 
if (isset($_GET['britbatID']))
{ $britbatID = $_GET['britbatID'];
}?>
<?php
mysql_select_db($database_britishforces, $britishforces);
$query_britbatname = "SELECT * FROM battalions
WHERE battalions.britbatID = '$britbatID'";
$rsbritbatname = mysql_query($query_britbatname, $britishforces) or die(mysql_error());
$row_britbatname = mysql_fetch_assoc($rsbritbatname);
?>
 
Every time I run editbritbat is fails and states :Undefined variable: britbatID in C:\xampp\htdocs\BFWIIFD\editbritbat.php on line 9
 
I am sure this is an easy answer but I am stuck and don't get what I am doing wrong.
 
Cheers
Richard

 

The variable $britbatID will only be defined if $_GET['britbatID'] is defined (ge your url is sitename.com/editbritbat.php?britbatID=1243). This is what this code does

<?php 
if (isset($_GET['britbatID']))
{ $britbatID = $_GET['britbatID'];
}?>

So if $_GET['britbatID'] does not exist then $britbatID will not be difined and so your get the undefined variable error.

 

You are also redirecting your POST request to a GET request when the form is being submitted to itselft. This seems unnecessary. Don't perform the redirect, just replace $_GET with $_POST

if (isset($_POST['britbatID']))
{
    mysql_select_db($database_britishforces, $britishforces);
    $britbatID = intval($_POST['britbatID']);
    $query_britbatname = "SELECT * FROM battalions WHERE battalions.britbatID = '$britbatID'";
    $rsbritbatname = mysql_query($query_britbatname, $britishforces) or die(mysql_error());
    $row_britbatname = mysql_fetch_assoc($rsbritbatname);

    // process result
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.