php - Conditionally create URLwith php and pass to jquery.ajax
问题描述
I have a page with an ajax form and want to control which form the user is served based on an url argument. I can read the url arg and decide which form the user needs but cannot figure out how to pass the url from php to javascript.
for example the user visits page based on example.com/index.php?utm_source=test
Then I check for the code and define the form the user should use
$utm_source = $_GET['utm_source'];
if ($utm_source = 'test'){
$formList='list[4]';
$formUrl= 'https://www.example.com/?p=asubscribe&id=4' ;
}
else {
$formList="list[2]";
$formUrl="https://www.example.com/?p=asubscribe&id=2";
Still in the header I add the url to ajax using json_encode
function submitForm() {
successMessage =
"success.";
data = jQuery("#subscribeform").serialize();
jQuery.ajax({
type: "POST",
data: data,
url: <?php echo json_encode("$formUrl") ?>;,
dataType: "html",
success: function (data, status, request) {
jQuery("#result")
.empty()
.append(data != "" ? data : successMessage);
jQuery("#email").val("");
},
error: function (request, status, error) {
alert("error.");
},
});
}
However the url that is generated is https:\/\/www.example.com\/?p=asubscribe&id=4
I also try the http_build_query($data)
but it doesn't include the https://domain, the form needs full url.
How can I fix the \/\/
issues so it's a valid URL?
解决方案
推荐阅读
- oracle-apex - 从计算中绑定标签 - oracle apex
- javascript - 将此数组中的数字替换为 %
- arrays - 使用C中的数组打印二维二叉树
- python - pip 可以在指定的本地目录中安装第一个搜索轮然后再安装 pypi 吗?
- r - 用新变量对不同时期进行分组
- api - 带有 curl 的 LinkedIn API?
- spring-boot - 如何在spring boot rest api中使用postman将带有一些变量的图像上传到mysql
- linux - cmake交叉编译链接提升
- google-apps-script - 匹配 X 的出现是 Y 但对应的 Z 缺失
- algorithm - 计算置换子集