当您需要将变量与 PHP 中的 $_POST 值连接时该怎么做
What to do when you need to concatenate a variable with a $_POST value in PHP
在前端我有一个隐藏的动态输入,它也有一个动态属性name
html echo '<input type="hidden" name="like_c_'.$pc.'" value="like" />';
php 变量在 attritute 内连接
在 html 中就像
<input type="hidden" name="like_c_abcde123.'" value="like" />
姓名="like_c_'.$pc.'
"
只是为了更容易理解上面的代码,html是粗体,php是前代码格式
在我的 php 后端,我需要得到这个:
$string = htmlentities($_POST['like_c']);//concatenate variable here
问题是如何连接以获得准确的 $_POST
名称
像这样:name="like_c_'.$pc.'
"
我的代码是通过 jquery serialize
发送的,也是动态生成的,前端有 db 值,这就是为什么我需要在后端连接
我不知道这是否是正确的方法
$string = htmlentities($_POST['like_c_'.$var]);
这里的问题是我认为它不会工作,因为 like_c_
会被视为 name="like_c_"
我认为这可能适合你。
foreach($_POST as $key => $value) {
if (strpos($key, 'like_c_') === 0) {
$key = htmlentities($_POST[$key]);
}
}
在前端我有一个隐藏的动态输入,它也有一个动态属性name
html echo '<input type="hidden" name="like_c_'.$pc.'" value="like" />';
php 变量在 attritute 内连接
在 html 中就像
<input type="hidden" name="like_c_abcde123.'" value="like" />
姓名="like_c_'.$pc.'
"
只是为了更容易理解上面的代码,html是粗体,php是前代码格式
在我的 php 后端,我需要得到这个:
$string = htmlentities($_POST['like_c']);//concatenate variable here
问题是如何连接以获得准确的 $_POST
名称
像这样:name="like_c_'.$pc.'
"
我的代码是通过 jquery serialize
发送的,也是动态生成的,前端有 db 值,这就是为什么我需要在后端连接
我不知道这是否是正确的方法
$string = htmlentities($_POST['like_c_'.$var]);
这里的问题是我认为它不会工作,因为 like_c_
会被视为 name="like_c_"
我认为这可能适合你。
foreach($_POST as $key => $value) {
if (strpos($key, 'like_c_') === 0) {
$key = htmlentities($_POST[$key]);
}
}