1

In a php project I need to add items to database, list them & allow the user to edit & update items using a single page.

This is my code to edit item link in HTML table

echo  '<a href="  '.$_SERVER['PHP_SELF'].'?name=edit&id='  .$rowCountry->CountryId .' " id="edit"  onClick="MyFunction()"> Edit </a>';

When a user click on the above link I need to hide Add button and display two new buttons to Update & Cancel the edit + display selected item name in a Text box to Edit.

To hide and display buttons I'm using jQuery and to display the item name i need to use PHP. Here when i put PHP code and reload the page with $_SERVER['PHP_SELF'] (as in above code), hiding & displying of buttons is lost after the page load. (If I remove the _SERVER['PHP_SELF'] code from link it hides and display buttons as expected (but no php code run))

How can I retain the page update by Javascript and run the PHP code?

I'm new to PHP am I missing something in my code?

1 Answer 1

2

$_SERVER['PHP_SELF'] is only a refrence to the php document itself, it doesnt contain any key/value pairs. try using:

echo  '<a href="  '.$_SERVER['PHP_SELF'].'?'. $_SERVER['QUERY_STRING'].
    '&name=edit&id='  .$rowCountry->CountryId .
        ' " id="edit" onClick="MyFunction()"> Edit </a>';

edit: i may have misunderstood the question. you may need something like this:

<?php
if(!empty($_GET[edit])){
    //echo code that u want to show AFTER they click the edit link
}else{
    //echo the code to show if they have NOT clicked the edit link
}
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer, Actually the PHP code runs fine, but the hiding of button is lost when page loads. (as I think its because, it runs the javascript client side-> hides buttons-> run php code in server-> loads the page from server-> button is back as it was).
you should only echo the buttons you need in php. you dont need to show/hide them with jquery... unless your using ajax?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.