作者SpringMud (呆呆。)
看板PHP
標題[請益] Ajax與PHP Simple HTML DOM Parser
時間Tue Mar 10 18:33:22 2015
請教各位大大,
小弟最近使用PHP Simple HTML DOM Parser去parse <a>裡的href,
我這樣寫可以正確跑出我預期的結果。
以下是我的code:
*File Name: parser.php
====================================================================
include( 'simple_html_dom.php' ); //PHP Simple HTML DOM Parser file
$msg = '<div><p><a href="
http://www.apple.com"></a></p></div>';
$html = str_get_html($msg);
$temp = "/123";
$html->find('a',0)->href = '
http://www.google.com'.$temp;
echo $html->find('a',0)->href; //OUTPUT =>
http://www.google.com/123
====================================================================
但是,如果我用ajax將index.php的變數丟到parser.php來處理,就會出現問題。
*File Name: index.php
====================================================================
<div id="test_area">
<div><p><a href="
http://www.apple.com"></a></p></div>
</div>
<button id="send_btn" type="button">Send</button>
<script>
$(document).ready(init);
function init(){
$('
#send_btn').click(function(){
var message = $('
#test_area').html();
$.ajax(
{
url: 'parser.php',
cache: false,
dataType: 'json',
type: "POST",
data: {message_ajax:message},
error: function(xhr) {
alert('ERROR');
},
success: function(response)
{
alert("Works!");
}
});
});
}
</script>
====================================================================
然後小改一下parser.php
*File Name: Parser.php
====================================================================
include( 'simple_html_dom.php' ); //PHP Simple HTML DOM Parser file
$msg = $_POST['message_ajax']; //改了這裡!!!
$html = str_get_html($msg);
$temp = "/123";
$html->find('a',0)->href = '
http://www.google.com'.$temp;
====================================================================
最後我去看<a>裡的href,竟然不會動,
依舊是原來的"
http://www.apple.com",
而不是"
http://www.google.com/123"。
求解!!!(跪
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 118.160.227.209
※ 文章網址: https://webptt.com/m.aspx?n=bbs/PHP/M.1425983613.A.8B3.html
1F:→ anest: 1.有跳出works的視窗嗎?2.你沒處理收到的資料啊..... 03/10 19:21
2F:→ SpringMud: 我用ajax POST data,並且用$_POST接資料 03/10 19:30
3F:→ Darkland: 你的 js 有處理 parser.php echo 出來的結果嗎? 03/10 22:12
4F:→ SpringMud: 有 03/11 01:10
5F:→ Darkland: 但你貼出來的部份並沒有 js 最後處理的方式? 03/11 21:11
6F:→ xzeertacat: 把$msg印出來 看收到什麼 03/13 00:42