I am trying to do a forum and trying to create a section where admin can create categories and sub categories. I have got my category page to work with a drop down menu but I am not sure how to get it to display the category id in the url? I thought that I might have to type something in the option value tag but it automatically sends me to the link once I have selected the tag instead of waiting for me to press the submit button:
<?php
include_once 'header2.php';
if(!$_SESSION['u_uid']) {
echo "<meta http-equiv='refresh' content='0;url=index.php?create_music_sub=notlogin>";
exit();
} else {
if($_SESSION['u_permission'] == 0){
echo "<meta http-equiv='refresh' content='0;url=header2.php?create_music_sub=nopermission'>";
exit();
} else {
$admin = 0;
$date = date("Y-m-d H:i:s");
$sql = "SELECT * FROM music_forum_cats WHERE admin = ?;";
$stmt = mysqli_stmt_init($conn);
if(!mysqli_stmt_prepare($stmt, $sql)) {
echo "SQL error";
} else {
mysqli_stmt_bind_param($stmt, "i", $admin);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$resultCheck = mysqli_num_rows($result);
echo 'You have'.$resultCheck.'results';
echo '<form action="create_music_sub_process.php" method="POST">';
echo '<table class="create_music_sub">
<tr>
<th>Category</th><td>';
echo '<select><option disabled selected>Pick a Category</option>';
while($row = mysqli_fetch_assoc($result)) {
$id = $row['id'];
echo '<option value="create_music_sub.php?cat='.$id.'">'.$row['category'].'</option>' ;
}
echo '</select></td>';
'</tr>';
echo '
<tr>
<th>Sub Category</th><td><input type="text" name="sub_cat"></td>
</tr>
<tr>
<th>Creator</th><td>'.$_SESSION["u_uid"].'</td>
</tr>
<tr>
<th>Date Created</th><td><input type="text" name="date" value='.$date.'></td>
</tr>
<tr>
<th></th><td colspan="2"><input type="submit" name="submit" value="Create Sub Category"></td>
</tr>
</table>
</form>';
}
}
}
?>
I have changed the following but shouldn't I be using $row? instead of $post?
echo '<form action="create_music_sub_process.php" method="POST">';
echo '<table class="create_music_sub">
<tr>
<th>Category</th><td>';
echo '<select name="create_music_sub"><option disabled selected>Pick a Category</option>';
while($row = mysqli_fetch_assoc($result)) {
$id = $row['id'];
echo '<option value="'.$id.'">'.$POST['create_music_sub'].'</option>' ;
}
echo '</select></td>';
'</tr>';
If you are not using an JS you will need to define the name parameter of the select and value of the option elements.
<select name="POST_NAME">
and <option value="'.$id.'" >
The form will direct you and the form data to create_music_sub_process.php
, where you can access the data stored in the $_POST
variable.
To access the select elements data use <?php echo $_POST['POST_NAME']; ?>
If you want to redirect on selecting the element, with jquery you could do something like:
<script type="text/javascript">
$(function(){
// on change of the element, send user to
$("#idToTarget").on('change', function(){
// change window location to the url
window.location.href = 'http://example.com/id=' + $('select' . $this).val();;
});
});
</script>