Abhishek Sinha
Abhishek Sinha

Reputation: 5123

store a button id in php variable

I made changes in my code as suggested. Here's my updated code:

<form  action="" method="POST">
        <div class="btn-group btn-group-justified" role="group" aria-label="...">
            <div class="btn-group" role="group">
                <input type="submit" class="btn btn-default" name="navbtnplace" value="<?=$country?>">
            </div>
            <div class="btn-group" role="group">
                <input type="submit" class="btn btn-default" name="navbtnplace" value="<?=$state?>">
            </div>
            <div class="btn-group" role="group">
                <input type="submit" class="btn btn-default" name="navbtnplace" value="<?=$city?>">
            </div>
            <div class="btn-group" role="group">
                <input type="submit" class="btn btn-default" name="navbtnplace" value="<?=$user_name?>">
            </div>
        </div>
        </form>
<?php
if(isset($_POST['navbtnplace'])) {
                    echo  $_POST['navbtnplace'];

                }
                ?>

But it refreshes the page as well. How can I get the button value without page refresh?

Upvotes: 2

Views: 3274

Answers (3)

Ahmad Asjad
Ahmad Asjad

Reputation: 823

No need of switch just call extact() funtion with value sent by form.

<?php
if(isset($_REQUEST['idVal'])) {
    $navBtnVal = $_REQUEST['idVal'];
    extract($navBtnVal);
}

function extract($id) {
    //some event
}
?>

<!-- Change action attribute to some other page if you want to send data to somewhere else.  -->
<!-- You also missed equal operator(=) in class -->
<!-- Give an extra attribute value and put its value as like id as below. Because php will receive your value attribute's data-->
<form method="post" action="">
    <input type="button" id="a" name="idVal" value="a" class="navbtnplace" />
    <input type="button" id="b" name="idVal" value="b" class="navbtnplace" />
    <input type="button" id="c" name="idVal" value="c" class="navbtnplace" />
    <input type="button" id="d" name="idVal" value="d" class="navbtnplace" />
</form>

Upvotes: 0

Muaaz Khalid
Muaaz Khalid

Reputation: 2249

PHP only take the form data - whatever is in value attribute So, you can't get the id value in PHP code. If you want to check which button is pressed, You can only check it using value attribute.

test.html

<html>
    <body>
        <form action="index.php" method="POST">
            <!-- other form fields -->
            <input type="button" id="a" name="idVal" value="a" class"navbtnplace">
            <input type="button" id="b" name="idVal" value="b" class"navbtnplace">
            <input type="button" id="c" name="idVal" value="c" class"navbtnplace">
            <input type="button" id="d" name="idVal" value="d" class"navbtnplace">
        </form>
    </body>
</html>

index.php

<?PHP
    echo $_POST['idVal'];
?>

NOTE: If you send an AJAX request to store the value and then submit the form (separately), you will lose the first value because, those two are the independent requests. But, SESSION and COOKIE will work in this scenario. once, a value is saved in session via AJAX or the normal form submission, It'll be saved Globally and you can access the saved values after refreshing the page.

Upvotes: 0

Hafidz Jazuli Luthfi
Hafidz Jazuli Luthfi

Reputation: 19

PHP not capable to catch html's id as form data. If you want catch the html's id and insert it to variable you must do a simple trick, insert button's value same as its id. So, your buttons should be like this:

<form method="post">
    <input type="submit" id="a" name="idVal" value="a" class"navbtnplace">
    <input type="submit" id="b" name="idVal" value="b" class"navbtnplace">
    <input type="submit" id="c" name="idVal" value="c" class"navbtnplace">
    <input type="submit" id="d" name="idVal" value="d" class"navbtnplace">
</form>

<?php

if(isset($_REQUEST['idVal'])) {
    ext($_REQUEST['idVal']);
}

function ext($id) {
   echo $id;
}

Please review your code at switch case process and you should not create function with has same name with native php function.

Upvotes: 1

Related Questions