Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

javascript - Changes to page are not saved (on page reload)

<div class='wrapper one Stone' id='main'>
<h1>Stone</h1>
<p>Price: $130.00 per Yard</p>
 <?php if($_SESSION[id]) {?> 
<button class="Stonebutton"> Delete Stone</button>
<script>; $(".Stonebutton").click(function(){$(".Stone").remove();}); </script> 
 <?php } ?> 
</div>

When im logged in i can see the "Remove Stone" button and when i click it, it goes away but when i reload the page it comes back. Any help? Thanks a bunch!Im also loading this into the html page with a seperate php file using this:

fwrite($PlantFile, "

<div class='wrapper one $trimmedname' id='main'>
<h1>$name</h1>
<p>Price: $price</p>
 <?php if($_SESSION[id]) {?> 
<button class="$trimmedbname"> Delete $name</button>
<script> $(".$trimmedbname").click(function(){$(".$trimmedname").remove();}); </script> 
 <?php } ?> 
</div>
");

fclose($PlantFile);

Also if anyone has a better solution on what to do, im all ears. Thanks for all your help!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In order to properly understand this, you need to understand the life-cycle of a web page.

  1. The browser makes a request to the server.
  2. The server responds to that request. From the large array of possible responses, we'll assume everything went OK and it responded with a web-page (status code: 200). Most times (and the case we will consider here), the response is HTML.

Excluding all the things that could go wrong, a server will always return the same result for the same request. However, sometimes, that's not enough and that's where we use JavaScript. With JavaScript, we can make changes to the returned HTML, without having to tell the server: create a new page, containing this mod.

.remove() is such a JavaScript method. It changes the HTML after it was returned. It removes an HTML element that was part of the initial response, without going back to server and removing the code that generated the element in the first place, during the request.

This means that the page will contain the element again when you refresh the page, because you're making the same request (you're back to step 1).

If web worked the way you seem to expect it to, anyone could open up the console and delete the entire website with a simple line of code:

document.body.remove();

Luckily, that's now how web works.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...