Jump to content

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


Gilwren
Go to solution Solved by Ch0cu3r,

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

 

Link to comment
Share on other sites

  • Solution

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
}
Edited by Ch0cu3r
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.