-1

I have a dynamic menu inside a form element code is here

<form method="GET" action="index.php">
<?php
display_menu(); // this function generates menu items
?>
</form>

after the menu is generated every menu item is a submit button of the above form I want to get input of a single element by name or id attribute of submit button, and load a post from database.

<form method="GET" action="index.php">
<input type="submit" name="page-1" id="page-1" value="page-1">
<input type="submit" name="page-2" id="page-2" value="page-2">
<input type="submit" name="page-3" id="page-3" value="page-3">
<input type="submit" name="page-4" id="page-4" value="page-4">
</form>

so when any input button is pressed the function display_post() is called. Code of the function is as follows:-

function display_post(){
$conn = mysqli_connect('localhost', 'root', '', 'posts') or die('cannot connect');
if($_SERVER['REQUEST_METHOD'] == 'GET' ){
$blog_post_id = $_GET["id"]; 
$sql = "SELECT * FROM blog_posts where id='blog_post_id' ";
$result = mysqli_query($conn, $sql) or die('cannot load');
while ($row = mysqli_fetch_assoc($result)){
if($row > 0){
echo '<div>'.$row['content'].'</div>';
}else echo 'no posts';
}
}
}

However, the display_post() method is called inside a content tag whereas the display_menu() is called inside another div. So the problem is I'm unable to get the id of the to submit button any help will be appreciated thanks in advance.

Gufran Hasan
  • 8,910
  • 7
  • 38
  • 51
Zubair
  • 1
  • 4

3 Answers3

0

When you click on the submit button you will get all inputs values of the form.

Do one thing put this statement print_r($_GET);exit; in display_post() as a first line and see what you get after clicking on submit button.

Note: 1. you want to get the value of clicked button then you should use javascript or jQuery and Ajax. 2. You can not get input fields value by fields ID. ex. if we have field <input type="submit" name="page-1" id="page-1" value="page-1"> then we can get its value as:

echo $_GET['page-1'];

page-1 is the input field name.

Gufran Hasan
  • 8,910
  • 7
  • 38
  • 51
  • see these solutions https://stackoverflow.com/questions/1200266/submit-a-form-using-jquery and https://www.johnmorrisonline.com/jquery-tutorial-submit-a-form-and-post-data-using-jquery-and-ajax/ – Gufran Hasan Oct 05 '18 at 09:46
  • I put the variable print_r($_GET) after the line $blog_post_id = $_GET["id"]; and get output Array ( [9] => html ) – Zubair Oct 05 '18 at 09:52
  • put it as a first line in function `print_r($_GET) ;exit();` then see th result would be `array("page-1"=>page-1,"page-2"=>page-2,"page-3"=page-3, "page-4"=>page-4)`. – Gufran Hasan Oct 05 '18 at 09:57
  • same output and have value as Array ( [9] => html ) but I am unable to collect value in variable $blog_post_id = $_GET["id"] or even using array index $blog_post_id = $_GET["0"] – Zubair Oct 05 '18 at 10:03
0

If I am guessing right what is your difficulty then

$blog_post_id = $_GET['id']; 
$sql = "SELECT * FROM blog_posts where id='".$blog_post_id."' ";//I modified this line
$result = mysqli_query($conn, $sql) or die('cannot load');
0

You can make 5 different forms for each button and change the action to

action="index.php?id=1" 

Then use $_GET['id']

Also change id='blog_post_id' " to id='$blog_post_id' "

Michael Lihs
  • 7,460
  • 17
  • 52
  • 85
Zack Heisenberg
  • 499
  • 6
  • 12