【发布时间】:2014-08-02 04:24:56
【问题描述】:
我正在尝试执行链接,以便该页面不会重定向,而只是执行链接..
我尝试了以下 curl pgm(对 curl 来说是全新的)
$ch = curl_init(); // Initializing
curl_setopt($ch, CURLOPT_URL,"http://api.smsgatewayhub.com/smsapi/pushsms.aspx?user=stthomasmtc&pwd=429944&to=9176411081&sid=STMTSC&msg=Dear Sam,%20choir%20practice%20will%20be%20held%20in%20our%20Church%20on%20July%2031%20at%208:00%20pm.%20Thanks,%20St.%20Thomas%20MTC!&fl=0&gwid=2"); // Set URI
curl_setopt($ch, CURLOPT_HEADER, 0); //Set Header
curl_setopt($ch, CURLOPT_TIMEOUT, 300); // Time-out in seconds
$result = curl_exec($ch); // Executing
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode != 200) {
$result = ""; // Executing
}
curl_close($ch); // Closing the channel
return $result;
但这并没有按预期工作...我尝试在 php 中使用 javascript 代码回显,但是由于该 php 文件是通过 ajax 调用的,因此该脚本没有被执行.. :(
调用 pgm
<div class="button_div">
<input type="button" onclick="send_sms()" value="Send SMS" class="submit">
</div>
和 javascript 函数
function send_sms()
{
var sms_msg=(document.getElementById('preview_box').value).trim();
/* var prayer_group_nm=(document.getElementById('prayer_group').value).trim();
if ((recepient==='prayer_grp')&&(prayer_group_nm==='select'))
{
alert('Select the appropriate Prayer group');
return;
}
*/
if (sms_msg==='')
{
alert ('Preview MSG prior to sending!');
}
else
{
myurl = '../sp/send_sms_sp.php';
parms="?recepient="+recepient+"&sms_msg="+sms_msg;
modurl = myurl+parms;
if (recepient==='prayer_grp')
{
modurl=modurl+"&prayer_group_nm="+prayer_group_nm;
}
response='sms_templates_response'; //setting div where response is being displayed
swtch='send_sms_state';
ajax_call.call();
//removing earlier checked state of radio buttons
$("input:radio").attr("checked", false);
document.getElementById('preview_box').value='';
//hiding out unwanted divs
$(".button_div").hide();
$("#preview").hide();
}
}
还有 ajax_call() 函数...
function ajax_call()
{
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.onreadystatechange=function()
{
if (xmlhttp.readyState===4 && xmlhttp.status===200)
{
if (swtch!=='preview')
{
document.getElementById(response).innerHTML=(xmlhttp.responseText);
}
else
{
raw_template=xmlhttp.responseText;
//do not move the below code to the calling function - as it has a bug.. will not work as expected, if done
modified_msg='';
get_modified_msg.call();
document.getElementById(response).value='Dear (name), '+modified_msg.trim()+'. Thanks, St. Thomas MTC!';
}
// return;
}
else
{
if (swtch!=='preview')
{
document.getElementById(response).innerHTML ='<img src="../animation/ajax-loader.gif">';
}
}
}
xmlhttp.open("GET", modurl, true);
xmlhttp.send();
} // return;
};
和被调用的php
<?php
//retreiving data from db and then below code
$sender_id='STMTSC';
$pwd='429999';
$url='http://api.smsgatewayhub.com/smsapi/pushsms.aspx?user=stthomasmtc&pwd='.$pwd.'&to='.$mob.'&sid='.$sender_id.'&msg='.$msg.'&fl=0&gwid=2';
echo $url.'<br>';
echo '<script src="../jquery/jquery-1.10.1.js">';
echo '</script>';
echo ' <script>';
// echo ' $(document).ready(function(){';
echo ' $.ajax({';
echo ' type: "POST",';
// url: "http://api.smsgatewayhub.com/smsapi/pushsms.aspx?user=stthomasmtc&pwd=429944&to=9176411081&sid=STMTSC&msg=Dear Sam,%20choir%20practice%20will%20be%20held%20in%20our%20Church%20on%20July%2031%20at%208:00%20pm.%20Thanks,%20St.%20Thomas%20MTC!&fl=0&gwid=2",
echo ' url: "'.$url.'" ,';
echo ' data: { message: "hello" }';
echo ' })';
echo ' .done(function( msg ) {';
echo ' alert( "script executed successfully ");';
echo ' this.preventDefault();'; // stops navigating from the page while <a> click or on submit click.
echo ' }); ';
//echo ' });';
echo 'window.alert("ending script")';
echo ' </script>';
//update of db as log
userid 和 pwd 在这种情况下无效; 因为它在 php 中的脚本是通过 ajax 调用的,所以事情不工作:( 能否请您提供一种方法来执行通过 ajax 调用的 php 页面中的链接;
提前非常感谢!
【问题讨论】:
-
你怎么打电话给
send_sms()? -
“执行链接”是什么意思?你的意思是当你点击链接时调用一个Javascript函数?使用
href="javascript:send_sms()" -
您应该在所有 AJAX 参数周围加上
encodeURIComponent。否则,如果它们包含空格或其他特殊字符,您将遇到问题。 -
@Barmar 感谢您的回复.. 是的,我通过按钮单击调用 send_sms() - onclick 事件...当我尝试直接执行 php 脚本时,其中的 javascript 工作;但是当我尝试通过 ajax 调用它时,它不起作用..(我的整个工作都停滞不前,因为我找不到出路:()你能告诉我我需要做的更正吗......当我用谷歌搜索时,它说我应该使用 eval() 函数..但不知道如何使用它..
-
将调用
send_sms的代码添加到您的问题中。
标签: javascript php ajax curl