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 ' Edit ';
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?
$_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
}
?>
Related
I'm looking to build a site which will have multiple options, for example;
NOTE** These are images not any forms or input type
Pick from one of the 5 images
Pick from one of the 3 images
Pick from one of the 2 images
Pick from one of the 6 images
and so on...
What I need to do is record which option (image) the user clicked on and then output a message.
For example if you picked a b c d, msg = hello
I'm not sure how to record this data, thanks!
You could link each image to a seperate page or reload the current page with a query string appended for example adding the following to your images as links
<img src="set1-image3.png" />
You could then save this information using PHP by processing only if the GET string contains the parameters image and set and could then split out which set/image has been clicked this way.
You could also use this to choose which image set to show on the screen (show set 2 if set=1 is in the query string etc.)
Using $_GET['set'] and $_GET['image'] you would have access to this information very easily in PHP.
For example you could do:
if( isset($_GET['image']) && isset($_GET['set']) ) {
echo "You clicked image" . $_GET['image'] . " from set " . $_GET['set'];
}
That would be the only pure PHP answer. The other way to do this is with Ajax or JavaScript but I'm guessing by your part about it being done in PHP this may not be what you want.
You could add a parameter to the link you click.
For example <img>
In page2.php you could read that variable using:
$_REQUEST["clickedImage"]
For example:
<p>The clicked image is: <?php echo $_REQUEST["clickedImage"] ?>
I've a database (image1) and using PHPMaker i created the graphic interface (image2). What i want is to add a button at the bottom of the page that will print the selected data from the selected fields (image3).
Can anyone tell me how to do that?
Do i have to use php code or a javascript?
Just add a print link by echoing the following to create a "Print" link on the page
echo "<a href='#' onclick='javascript:window.print();'>Print Page</a>";
Button click will make the browser present the user with the default print dialog.
i have simple coding problem. i have created a page with textbox and share button. the page also contains one Points up button.
i had a problem with that points up button that when the user click on that button and refresh the page ... a window ask for resend of information
for that i have used following code which works fine.
`header('Location: samepageurl.php');
exit;`
but the problem with above code is when user scroll down page and click the button. the page automatically scrolls up. and user have to manually scroll it down.
what i want is the page should refresh but it should be on the same location where it was.
if the problem is still unclear please refer the following images
You can set a fragment identifier.
eg:
<a name="points_up"></a> <!-- this needs to be near that button, the page will scroll exactly where the element is -->
and redirect him to:
header('Location: samepageurl.php#points_up');
die;
Mihai answer is correct, but as you said that fragment identifier is not working because each user has points up button, you can pass user id as a fragment identifier and make a hidden(display : none;) <a> tag and pass the user id in front of each user...
Like this:
You can set a prefix before a user id too (optional)
<a name="pu12345" style="display: none;"></a>
<?php
header('Location: whatever.php#pu12345');
exit;
?>
You can send the request via ajax instead relying on the normal form submission. That will not affect the scrolling of the current page.
Add this line at the bottom of your page before the the <\body> tag
<button id="PageRefresh">Refresh a Page in jQuery</button>
<script type="text/javascript">
$('#PageRefresh').click(function() {
location.reload();
});
</script>
I use pagination class to generate the following table, book titles are stored in a database
Title Action
New book for you Edit Delete
How to become a manager Edit Delete
Learn PHP in 10 minutes Edit Delete
< 1 2 3...10 Last>
Each Edit or Delete is a link created as Edit
In order to move to the edit page to update book title (edit and save into mysql table) that I need to use $_GET to retrieve $title of the correct clicked Edit link, I wonder how the href in case of a single page likely to be href='book/details?title=$title' becomes ?
If you generate your Markup through PHP you could do as you said.
foreach ($titles as $title) {
echo 'title';
}
Otherwise you would just type the url in your href.
<a href="book/details?title=actualTitleName">
and get it serverside with
$_GET["title"]
Ok, i have some data that is being populated via a php query to a database. I am getting comments that a user enters. I am displaying the returned data in a variable as shown below. If a comment does not exist then it would display an "add comment" link. The comment is just displayed as text at first. If a comment exists then it would display the comment along with an "edit" comment" link.
This is the functionality i am looking to have: 1) when you click on "add comment" an input field would be displayed to the left of the "add comment" link and that link would then turn in to two links that read "save" and "cancel". I know how to do the save link but i would like to know how to make the "cancel" link revert the area back to just having the "add comment" link. 2) when you click on "edit comment" then i would like the same functionality with an input field and the "save" and "cancel" links; however, i would like the input field to be pre-populated with the comments text that is being pulled via the comments variable. I'm thinking maybe this can be done with some css display toggling none and block/inline. Not sure exactly though. Any help is greatly appreciated!
PHP Code:
if(!$comments){
echo "<span style='font-size:12px;'>[<a href='#'>Add a Comment</a>]</span>";
}else{
echo "<span style='font-size:12px;'>NOTES: " . $comments . " [<a href='#'>Edit Comment</a>]</span>";
}
One final question: how should i handle if the comments have special characters or quotes? Actually i guess i'll need to worry about those before teh variable is created, correct? How do i do that? Thanks so much!
UPDATE: I appreciate the help on handling special characters, but i really need the first two questions answered more. Thanks for any more help anyone can provide me!
answer to 1st and 2nd question :
you can play with javascript or jquery....for eg
make 2 divs
<div id="addcommentBox" style='font-size:12px;'>[<a onclick="$('#addcommentBox').hide();$('#savecommentbox').show();">Add a Comment</a>]</div>
<div id="savecommentbox"><input type="text"/>Save<a onclick="$('#addcommentBox').show();$('#savecommentbox').hide();">Cancel</a></div>
give css property to #savecommentbox as display:none;
although there are many ways this is the simple one...for edit also you can follow the same code.just give the input value as $comments
You have to pass the variable from htmlspecialchars() before echoing them.
echo "<span style='font-size:12px;'>
NOTES: " . htmlspecialchars($comments) . "
[<a href='#'>Edit Comment</a>]
</span>";
<textarea name="comment"><?php echo htmlspecialchars($comments);?></textarea>
you can use htmlentities
one answer as many have given is htmlspecialchars().
when you click on the add comment link . show the text area. when you click on the cancel, do a hide of the text area and show the add comment link again. i feel if u use jquery u can use show / hide functions.