How to get submit button value in php

Questions : Get submit button value from multiple buttons php

2022-10-07T01:16:10+00:00 2022-10-07T01:16:10+00:00

878

I have a dynamic menu inside a form element anycodings_submit 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 anycodings_submit is a submit button of the above form I want anycodings_submit to get input of a single element by name or anycodings_submit id attribute of submit button, and load a anycodings_submit 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 anycodings_submit function display_post() is called. Code of anycodings_submit 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 anycodings_submit inside a content tag whereas the anycodings_submit display_menu() is called inside another anycodings_submit div. So the problem is I'm unable to get the anycodings_submit id of the to submit button any help will be anycodings_submit appreciated thanks in advance.

Total Answers 3

30

Answers 1 : of Get submit button value from multiple buttons php

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

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

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

echo $_GET['page-1'];

page-1 is the input field name.

0

2022-10-07T01:16:10+00:00 2022-10-07T01:16:10+00:00Answer Link

mRahman

5

Answers 2 : of Get submit button value from multiple buttons php

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

action="index.php?id=1" 

Then use $_GET['id']

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

0

2022-10-07T01:16:10+00:00 2022-10-07T01:16:10+00:00Answer Link

jidam

4

Answers 3 : of Get submit button value from multiple buttons php

If I am guessing right what is your anycodings_dynamic 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

2022-10-07T01:16:10+00:00 2022-10-07T01:16:10+00:00Answer Link

miraj



PHP isset() function

PHP isset() function is used to check if a variable has been set or not.
This can be useful to check the submit button is clicked or not.
The isset() function will return true or false value. The isset() function returns true if variable is set and not null.

PHP isset() function syntax

isset(“variable”);

PHP isset() Example

<?php

$str = 'meera'; 
var_dump(isset($str));

?>

The output is :

boolean true

lets another php form example to understand more about isset() function.

Here, we are do sum of two values, design php form with two textbox and one button control like :

<html>
<head>
<title>PHP isset() example</title>
</head>
<body>

<form method="post">

Enter value1 :<input type="text" name="str1"><br/>
Enter value2 :<input type="text" name="str2"><br/>
<input type="submit" value="Sum" name="Submit1">

<?php
$sum=$_POST["str1"] + $_POST["str2"];
echo "The sum = ". $sum;
?>

</form>
</body>
</html>

When we run above php example while first time page load it will show error like below:

How to get submit button value in php
PHP isset() function example

In above code add isset() function code to remove above error while page loading.

<html>
<head>
<title>PHP isset() example</title>
</head>
<body>

<form method="post">

Enter value1 :<input type="text" name="str1"><br/>
Enter value2 :<input type="text" name="str2"><br/>
<input type="submit" value="Sum" name="Submit1"><br/><br/>

<?php
if(isset($_POST["Submit1"]))
{
$sum=$_POST["str1"] + $_POST["str2"];
echo "The sum = ". $sum;

}
?>

</form>
</body>
</html>

The output of isset() function php example is :

How to get submit button value in php
PHP isset() function example

How get value from submit in PHP?

Use isset() method in PHP to test the form is submitted successfully or not. In the code, use isset() function to check $_POST['submit'] method.

How do I get form values on submit?

To get input values on form submit in React: Store the values of the input fields in state variables. Set the onSubmit prop on the form element. Access the values of the input fields in your handleSubmit function.

Can a submit button have a value?

Submit buttons don't participate in constraint validation; they have no real value to be constrained.

How do you tell which submit button was clicked in PHP?

PHP isset() function is used to check if a variable has been set or not. This can be useful to check the submit button is clicked or not. The isset() function will return true or false value. The isset() function returns true if variable is set and not null.