Jayson Lacson
Jayson Lacson

Reputation: 93

How to pass php variable (id) using button going to other form

Sorry for kinda idiot, I just want to ask if possible to pass a php variable using button. for example I have a list of records with update, contact, delete buttons at the end of row. when I click the update it will go to update page where i can get the id that passes through the button.

foreach($director->results() as $director){

                        $id = $director->id;

                        echo "<tr>  
                                <td>$director->director</td>  
                                <td>$director->agent_name</td>  
                                <td>$director->apz_account</td>  
                                <td>$director->api_account</td>  
                                <td>$director->wupos_tid</td>  
                                <td>$director->translink_tid</td>  
                                <td>$director->island_name</td>  
                                <td>$director->region</td>  
                                <td>$director->province</td>
                                <td>$director->city</td>
                                <td>$director->address</td>
                                <td>$director->landmark</td>
                                <td width='200'>

<button class='btn btn-mini btn-info' value='$director->apz_account'>Update</button>
<button class='btn btn-mini btn-info'>Contact</button>
<button class='btn btn-mini btn-info'>delete</button>"; //end the echo command

}

Upvotes: 2

Views: 12794

Answers (3)

HaRsH
HaRsH

Reputation: 313

try this make a function and then do like this

<button onclick="updateme('<?php echo $director->apz_account ?>')" class='btn btn-mini btn-info' value='$director->apz_account'>Update</button>

now the function is

function updateme(id){

//alert(id); just to get the id
window.location.href = 'your_page_url.php?id='+id;

}

make your code like this when you go to our_page_url.php use header there to load your current page

do your code likes this and then you get alert...
once you get alert page reload with given href and do your query
to complete the update...!!..
NOTE:-here no need to use value attr. because you get value in function

echo "<button onclick=\"updateme('".$director->id ."')\"  class='btn btn-mini btn-info' value=".$director->id ." >Update</button>";

Upvotes: 0

Abhinav
Abhinav

Reputation: 8168

When the user clicks the submit button, trigger an event in jquery

then get the id of that row using the attr property and pass it as a query string to that php page and from that php page you can get the value of that id and you can do any manipulations

For ex:

$("#button").click(function(){

var id=$(this).attr("id");
//Use jquery's ajax method here to

$.ajax(
                {
                    // The link we are accessing.
                    url: "your url?id="+id,

                    // The type of request.
                    type: "get",

                    // The type of data that is getting returned.
                    dataType: "html",

                    error: function(){
                        ShowStatus( "AJAX - error()" );

                        // Load the content in to the page.
                        jContent.html( "<p>Page Not Found!!</p>" );
                    },

                    beforeSend: function(){
                        ShowStatus( "AJAX - beforeSend()" );
                    },

                    complete: function(){
                        ShowStatus( "AJAX - complete()" );
                    },

                    success: function( strData ){
                        ShowStatus( "AJAX - success()" );

                        // Load the content in to the page.
                        jContent.html( strData );
                    }
                }                           
                );

});

Upvotes: 0

ponciste
ponciste

Reputation: 2229

you have to work with <form> and passing the id of director with an hidden <input> to the page in which you want to do the update

try this way:

<form method="POST" action="yourPage.php">
    <input type="hidden" name="id_director" value="<?= $id ?>" />

    <input type="submit" value="Update" />
</form>

then in yourPage.php

$id_director = $_POST['id_director'];

//do your query
"UPDATE director set foo = 'foo' where id = $id_director";

hope this helps

Upvotes: 3

Related Questions