gpt4 book ai didi

javascript - 如何将 PHP 结果创建的字符串提供给我的 javascript

转载 作者:行者123 更新时间:2023-12-03 00:27:23 25 4
gpt4 key购买 nike

我想在我在变量选择器上指定的输入字段中创建一个单词检查脚本。单词列表是从数据库中提取的。我已经提供了数据库的查询。数据库的结果是

>Array
>(
>[0] => Skype
>[1] => Phone
>[2] => Whatsapp
>[3] => Mobile
>[4] => Gmail
>[5] => email
>[6] => viber
>)

我正在使用上面的单词在我的 javascript 中为 if 语句创建字符串。 $a 的结果是

 $(selectors)[0].value.search(/Skype/i) !== -1 || $(selectors) 
[0].value.search(/Phone/i) !== -1 || $(selectors)
[0].value.search(/Whatsapp/i) !== -1 || $(selectors)
[0].value.search(/Mobile/i) !== -1 || $(selectors)>>
[0].value.search(/Gmail/i) !== -1 || $(selectors)
[0].value.search(/email/i) !== -1 || $(selectors)
[0].value.search(/viber/i) !== -1

之后,我尝试将此字符串作为脚本中的条件传递,它每次都会执行警报。我希望它仅在单词匹配时执行。

我已经尝试过写作

$(selectors)[0].value.search(/Skype/i) !== -1 || $(selectors) 
[0].value.search(/Phone/i) !== -1 || $(selectors)
[0].value.search(/Whatsapp/i) !== -1 || $(selectors)
[0].value.search(/Mobile/i) !== -1 || $(selectors)
[0].value.search(/Gmail/i) !== -1 || $(selectors)
[0].value.search(/email/i) !== -1 || $(selectors)
[0].value.search(/viber/i) !== -1

在 if 语句内,它工作得很好,但我想这样做的原因是我不想回到代码并在每次我想添加单词 < 时在 if 语句内添加另一行/p>

<?php
//testing
$words = array();
$a='';
foreach($db->getRecordSet('SELECT * FROM conversationKeywords WHERE conStatus = :conStatus',array(':conStatus'=>1)) as $results){
array_push($words,$results['conKeyWord']);
}
/* //DEVELOPER DEBUGGIN PURPOSES ONLY, UNCOMMENT ON DEVELOPER MACHINE
echo '<pre>';
print_r($words);
echo '</pre>';
*/
foreach($words as $values){
if(!next($words)) {
$a.= '$(selectors)[0].value.search(/'.$values.'/i) !== -1';
}
else{
$a.= '$(selectors)[0].value.search(/'.$values.'/i) !== -1 || ';
}
}
//echo $a;
?>

<script>
var selectors = "#msg_text,#Message_Text,#job-desc";
var conditions = <?php echo json_encode($a) ?>;
console.log(conditions);
$("body").on("keyup",selectors,function(conditions){

if(warning !== true){

//var v = $('#msg_text').val();

if (conditions){

//alert("Warning! For safety and quality assurance, Please note that we kindly request all parties to communicate only through us.");

alertify
.alert("Warning!","For safety and quality assurance,we kindly request that all parties communicate ONLY through us.", function(){
alertify.message('OK');});
//warning = true;
}
}
});

</script>

最佳答案

我建议使用正则表达式更改,而不是为每个搜索词一遍又一遍地编写 $(selectors)[0].value.search(/SEARCHTERM/i) !== -1 :

$(selectors)[0].value.search(/(SEARCHTERM_1|SEARCHTERM_2|SEARCHTERM_3)/i) !== -1

您可以使用 PHP implode函数从 $words 变量创建搜索词字符串。另外,如果您想获取当前焦点输入元素的值,您应该使用 $(this).val() 而不是 $(selectors)[0].value .

你的完整代码应该是这样的:

<?php
//testing
$words = array();
foreach($db->getRecordSet('SELECT * FROM conversationKeywords WHERE conStatus = :conStatus',array(':conStatus'=>1)) as $results){
$words[] = $results['conKeyWord'];
}
?>

<script>
var selectors = "#msg_text, #Message_Text, #job-desc";
$("body").on("keyup", selectors, function(){
if ($(this).val().search(/(<?= implode("|", $words) ?>)/i) !== -1) {
alertify.alert("Warning!","For safety and quality assurance,we kindly request that all parties communicate ONLY through us.", function(){
alertify.message('OK');
});
}
}
</script>

注意:$words[] = $results['conKeyWord'];array_push($words,$results['conKeyWord']); 相同,但它更快更干净。

关于javascript - 如何将 PHP 结果创建的字符串提供给我的 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54027914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com