I need some help, I made this php script and it posts perfectly, but as expected it refreshes the whole page and I need to refresh just this php file and return the results of the chosen city and selected="selected" option in the form so I can use it as many times I want, but with partial refresh, not the whole site. Thanks in advance. Here is the code:
<?php
$city='London';
$selected= array('selected="selected"','','');
if(isset($_POST["cities"])){
$city=$_POST["cities"];
switch($city)
{
case 'London': $selected[0]='selected="selected"'; $selected[1]=''; $selected[2]=''; break;
case 开发者_运维技巧'Manchester': $selected[0]=''; $selected[1]='selected="selected"'; $selected[2]=''; break;
case 'Liverpool': $selected[0]=''; $selected[1]=''; $selected[2]='selected="selected"'; break;
default: break;
}
}
?>
<html>
<head>
</head>
<body>
<div id="change">
<form name="ch_city" id="ch_city" method="post">
<select name="cities" id="cities" onchange="document.ch_city.submit();">
<option value="London" <?php echo $selected[0]; ?>>London</option>
<option value="Manchester" <?php echo $selected[1]; ?>>Manchester</option>
<option value="Liverpool" <?php echo $selected[2]; ?>>Liverpool</option>
</select>
</form>
<p>You've chosen: <?php echo $city; ?>.</p>
</div>
</body>
</html>
When doing AJAX work, especially with beginners a JavaScript library is highly recommended, it will ensure your code is cross-browser compatible and handle a lot of the dirty work. jQuery is generally a good place to start. The documentation is great and will get your JavaScript needs sorted. The idea is basically that you can load data from the server without refreshing the page. Good luck!
John, your problem is a bit more complex than it seems.
The root problem I see here is the structure of your code. Although it's very common to see PHP mixed in with HTML and some JavaScript thrown into the mix all in one file it's a recipe for disaster and it does not scale. In this case it doesn't scale to make the page more AJAX-y.
You should separate your PHP from your HTML and your HTML from your JavaScript. By separate I mean you should have a PHP file that has ONLY PHP in it, and an HTML file that has MOSTLY HTML (with the exception of some PHP or other template directives to tell it, for example, "this options tag is supposed to be selected".)
I wouldn't suggest you try and implement all of this on your own, what I would suggest is to look at frameworks like CakePHP that do this separation for you and guide/force you to do things "the right way." The main frameworks have pretty large communities and are well documented so when you run into a problem like "how do I AJAX this action?" chances are someone's already done it and it's supported by the framework.
Sorry for not answering your actual question but I feel like this answer will help you with more problems down the road.
I think you misunderstand what Ajax is. Ajax is a essentially a couple of javascript functions. Javascript is a client side technology and thus cannot be "put in php" besause PHP is executed on the server side.
EDIT : Like the poster before me I recommend checking out Jquery which helps a whole bunch for Web beginners.
精彩评论