【发布时间】:2014-12-17 06:32:55
【问题描述】:
我正在使用 ajax 脚本通过单击按钮调用 php 页面 "MAINPAGE.PHP",该页面包含一个 echo 语句到一个 id 为 "YOURDIV" 但它不调用 "MAINPAGE.PHP" 的 div 中,请参阅下面的代码和直接在你的 localhost 上运行它:
新更新:
<!DOCTYPE html>
<html>
<head>
<script>
function yourFunction($x)//passed $url as $x as you said
{
var xmlhttp = new XMLHttpRequest();
var url = "mainpage.php";// url to where to send
var params = "url=<?php echo $x;?>"; // here we add the php var $url as an 'url' request parameter
xmlhttp.open("POST", url, true);
//Send the proper header information along with the request
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", params.length);// add the length into header
xmlhttp.setRequestHeader("Connection", "close");
xmlhttp.onreadystatechange = function() {//Call a function when the state changes.
if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
alert(xmlhttp.responseText); // or do some other stuff
}
}
xmlhttp.send(params);// requesting with parameters. params contains value of url
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("POST","mainpage.php",true);
xmlhttp.send();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
$('.container').html(xmlhttp.responseText);
}
}
}
</script>
</head>
<body>
<!--this is the main form,if we press submit button four divs are formed , basically designing part-->
<form method="POST" action="#">
<input type="text" name="search"><input type="submit" name="submit">
</form>
<?php
if(isset($_POST['submit']))
{
$button = $_POST ['submit'];
$search = $_POST ['search'];
$con=mysql_connect("localhost","root","");//connecting from database
if(!$con)
{
die("error:".mysql_error());
}
mysql_select_db("database",$con);
$query="SELECT * FROM tablename ";
$result=mysql_query($query);//tablename contains two fields of four records: 1-S.no, 2- URL like www.google.com, yahoo, bing,rediff
while($runrows = mysql_fetch_assoc($results))// runrows is array with all the table content
{
$url = $runrows ['URL']; //$url now contains url from the database
echo"<div class='main' style='position:relative;border:1px solid #A5BEBE;background- color:#E7EFEF;width:84%; '>
<b> sometext:-</b><a href='#'>$url</a><br>
</p>
<!-- passed $url in yourfunction as you said-->
<input type='submit' value='expand' class='myButton' name='button1' onclick='yourFunction($url);' style='position:absolute;left:85%;top:4%;background:#B20000;color:white;width:70px;height:20px;font- size:15px;' >";
echo "<div class='container' id='yourdiv' style='background:white; '></p>";
echo "</div>";
echo "</div>";
}
mysql_close($con);
}
?>
</body>
</html>
这是我在 mainpage.php 中使用 $url 的方式
<?php
echo 'the response is ', $_POST['url'];//here i want the value of $url for further working
$z=array("google","yahoo","bing","rediff");
for($i=0;$i<=3;$i++){
if (ereg(".*($z[$i]).*", $url))
{
switch($i)
{
case 0:
$a=fopen($url,"r");//opening google site
echo "you have entered"$a;//you have entered www.google.com
break;
case 1:
$a=fopen($url,"r");
echo "you have entered"$a;
break;
case 2:
$a=fopen($url,"r");
echo "you have entered"$a;
break;
case 3:
$a=fopen($url,"r");
echo "you have entered"$a;
break;
}
}
}
?>
当您运行上面的代码时,您会看到四个带有四个按钮的 div,因此,每当我按下四个按钮中的任何一个时,mainpage.php 都必须将消息回显到所需的 div 中,但不是。请建议任何人我应该怎么做。
提前谢谢。 @igor,如果您不了解任何内容,我会向您解释。我发送给您只是为了理解这个冗长但易于理解的概念。
更新1:
1.- 我已经粗略地尝试了这段代码来传递 $url 及其工作::
<script>
function sendValue(id){
window.location='edit.php?ID=' + id;
}
</script>
<?php $url="hello";?>
<form method="POST" action="#">
<input type="button" onclick="javascript:sendValue('<?php echo $url; ?>')>
</form>
但我的整个代码都在 php 中,所以我尝试了这个不起作用::
<script>
function sendValue(id){
window.location='mainpage.php?ID=' + id;
}
</script>
<?php
$url="jhdbvkjsbd";
echo '<form method="POST" action="#">
<input type="button" onclick="javascript:sendValue($url);">
</form>';
?>
所以请解释如何以正确的方式传递 $url,但应该在 php 中。
2.- 其次在 mainpage.php 中的 switch case 、ereg 函数和 $a=fopen($url,"r");当我直接在while循环中加载这个mainpage.php时运行良好,方法是将它包含在php中,而不是在点击按钮后加载它,请参阅下面的代码:
while($runrows = mysql_fetch_assoc($results))// runrows is array with all the table content
{
$url = $runrows ['URL']; //$url now contains url from the database
echo"<div class='main' style='position:relative;border:1px solid #A5BEBE;background- color:#E7EFEF;width:84%; '>
<b> sometext:-</b><a href='#'>$url</a><br>
</p>
<!-- passed $url in yourfunction as you said-->
<input type='submit' value='expand' class='myButton' name='button1' onclick='yourFunction($url);' style='position:absolute;left:85%;top:4%;background:#B20000;color:white;width:70px;height:20px;font- size:15px;' >";
echo "<div class='container' id='yourdiv' style='background:white; '></p>";
include('mainpage.php'); //this statement directly loads mainpage.php and shows data in this div but i want the data to be shown after clicking the button
echo "</div>";
echo "</div>";
}
mysql_close($con);
}
如果您可以在 mainpage.php 中访问此 $url 变量,那么希望它可以工作。
最新更新:
function showRSS(str)
{
$('#loading').html('<img src="Preloader_1.gif"> loading...');
var xmlhttp = new XMLHttpRequest();
var url = "mainpage.php?q=+str";// url to where to send
var params = "url="; // here we add the php var $url as an 'url' request parameter
xmlhttp.open("POST", url, true);
//Send the proper header information along with the request
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", params.length);// add the length into header
xmlhttp.setRequestHeader("Connection", "close");
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
alert(xmlhttp.responseText);
}
}
xmlhttp.send(params);
var xmlhttp;
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("POST","mainpage.php?q="+str,true);
xmlhttp.send();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
$('.container').html(xmlhttp.responseText);
}
}
}
我想用 class="main" 显示在主 div 中加载图像,直到内容加载到容器 div 中,并且一旦加载该图像应该自动淡出,请告诉我我在哪里做错了
echo"<div class='main' style='position:relative;border:1px solid #A5BEBE;background-color:#E7EFEF;width:84%;'><div id='loading'>
</div>
<b> sometext:-</b><a href='#'>$url</a><br>
</p>
<!-- passed $url in yourfunction as you said-->
<input type='submit' value='expand' class='myButton' name='button1' onclick='yourFunction($url);'style='position:absolute;left:85%;top:4%;background:#B20000;color:white;width:70px;height:20px;font- size:15px;' >";
echo "<div class='container' id='yourdiv' style='background:white; '></p>";
echo "</div>";
echo "</div>";
<style>
#loading {
width: 100%;
height: 100%;
top: 0px;
left: 0px;
position: fixed;
display: block;
opacity: 0.8;
background-color: #000;
z-index: 99;
text-align: center;
}
#loading-image {
position: absolute;
top: 40%;
left: 45%;
z-index: 100;
}
</style>
【问题讨论】:
-
id始终是唯一的,而在for loop中它实际上并不像。 -
是的,我忘记了,那我应该使用 class 而不是 id ,是否可以,即 document.getElementByclass("container").innerHTML=xmlhttp.responseText;
-
如果你打算使用 jQuery,请坚持使用 jQuery。 JQuery 支持 Ajax,它为您处理所有跨浏览器的东西。 api.jquery.com/jquery.ajax
-
jquery 仅用于设计问题,但对于按钮,我使用 ajax 调用。请解释我是否使用 class 而不是 id 可以吗?
-
你不应该有重复的 ID,所以从这个意义上说,是的。
标签: javascript php ajax