![](/img/trans.png)
[英]How to get the value of a particular input field using id in the html templates
[英]How to get a particular value from input field
我的用户可以从reverbnation.com添加他的任何音频。 因此,用户可以复制其嵌入代码并将其粘贴到我的表单输入中。 我希望我的表单从输入文本中仅得到两个单词。
例如:-混响嵌入代码如下:
<iframe class="widget_iframe" src="https://www.reverbnation.com/widget_code/html_widget/artist_4859830?widget_id=50&posted_by=label_265034&pwc[design]=customized&pwc[background_color]=%23d4c79f&pwc[included_songs]=0&pwc[song_ids]=23591473&pwc[photo]=0%2C1&pwc[size]=undefined" width="100%" height="100%" frameborder="0" scrolling="no"></iframe>
因此,如果我的用户将上面的嵌入代码粘贴到表单输入字段中,那么我想从上面的文本中收集两个单词以将其保存在我的sql数据库中。
4859830
(... artist_4859830?...) 23591473
(...&pwc [song_ids] = 23591473 ...) 请给我一个指导; 如何从输入字段中收集2个以上ID。
我的sql数据库:id | 用户名| 艺术家| 歌曲
我的表单页面:
if(isset($_POST['submit'])){
$code = mysqli_real_escape_string($dbh, $_POST['code']);
$userid = mysqli_real_escape_string($dbh, $_POST['userid']);
$artist = How to get artist id (4859830) from $code
$song = How to get song id (23591473) from $code
$result = mysqli_query($dbh,"INSERT INTO user_aideo (id, userid, artist, song) values('', '$userid', '$artist', '$song')");
}
<form id="form" method="post" action="">
<input type="text" class="form-control" name="code"/>
<input type="hidden" name="userid" value="<? echo $session->userid; ?>"/>
<input type="submit" name="submit" id="submit" value=" Submit "/>
</form>
我认为这对于正则表达式将是一个很好的用例。
对于您的特定方案,您可以一次执行捕获两个数字。 您只需要使用preg_match
函数。
if (preg_match("/(?:.*)artist_([0-9]*)\?(?:.*)song_ids]=([0-9]*)/i", $code, $matches)) {
print 'artist: ' . $matches[1] . '<br />';
print 'song: ' . $matches[2];
}
这适用于您给定的代码示例。 它将歌手编号放在$matches[1]
,而歌曲编号放在$matches[2]
。
我很乐意直接在if语句中引用它们,因为正则表达式具有两个捕获组,因此如果preg_match
返回true
,则在$matches
数组中将始终有3个索引。 如果在if语句之外直接调用preg_match
,则需要遍历$matches
数组,以避免未定义的索引错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.