Gilwren Posted January 6, 2014 Share Posted January 6, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/285134-help-with-_post-_get-i-am-a-total-newbie/ Share on other sites More sharing options...
Solution Ch0cu3r Posted January 6, 2014 Solution Share Posted January 6, 2014 (edited) 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 January 6, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/285134-help-with-_post-_get-i-am-a-total-newbie/#findComment-1464051 Share on other sites More sharing options...
Gilwren Posted January 6, 2014 Author Share Posted January 6, 2014 Thanks that is a great help. Quote Link to comment https://forums.phpfreaks.com/topic/285134-help-with-_post-_get-i-am-a-total-newbie/#findComment-1464061 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.