drcmg28484 2017-08-11 07:39
浏览 289
已采纳

如何从bootstrap输入标签获取值,然后插入到mysql数据库中

I used bootstrap input-tags in my site. but I can't just handle the tags' values as they are rendered in a span tag with the same class names. I want to get values of each rendered tag and then insert into MySQL database using php and jQuery. what I have for now is:

<head>
<!-- botstrap-tags css and js -->
<link rel="stylesheet" href="bootstrap-tags/css/bootstrap-tagsinput.css" type="text/css">
<script src="bootstrap-tags/js/bootstrap-tagsinput.js"></script>
</head>

<form action="" method="post">
<div class="form-group">
<label>Tags</label>
<input type="text" name="tags" data-role="tagsinput" value="sport, politics, business">
</div>
<div class="form-group">
<input type="submit" value="add tags">
</div>
</form>

when I'm done with adding my tags, it generates tags as:

<span class="tag label label-info">
"sport" 
<span data-role="remove">
::after
</span>
</span>
<span class="tag label label-info">
"poltics" 
<span data-role="remove">
::after
</span>
</span>
<span class="tag label label-info">
"business" 
<span data-role="remove">
::after
</span>
</span>

this is my php script

<?php
$dbhost = 'localhost:3306';
$dbuser = 'root';
$dbpass = '';
$dbname = 'web';
$link = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
if(! $link ) {
echo 'Connected failure<br>';
}
if (isset($_POST['submit'])) {
$tags=$_POST['tags'];
$sql = "INSERT INTO `tags`(`name`) VALUES ('$tags')";
if (mysqli_query($link, $sql)) {
// alert success message
echo "tags added successfully";
}
else {
echo "Error: " . $sql . "<br>" . mysqli_error($link);
}
}
?>

anyone can help? thanks

  • 写回答

2条回答 默认 最新

  • duandong9195 2017-08-11 08:24
    关注

    I'm a little confused as to what you want, but I will try to answer based on the information as I understood it.

    jQuery is required for my answer.

    https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js

    Create the HTML form with the POST method to send the data to the PHP file.

    <form action="your_file.php" method="POST">
        <input name="tag" type="text">
        <input name="submit" type="submit">
    </form> 
    

    In your PHP file, using the $_POST[] superglobal, get the input data.

    $tagValue = $_POST['tag'];
    

    Finally, create the SQL command and query the database.

    $sql = "INSERT INTO table('name') VALUES('$tagValue')";
    $result = mysqli_query($link, $sql);
    
    if (!$result)
    {
        echo('failure');
    } else {
        echo('success');
    }
    

    EDIT: I believe I understand the question now.

    To get the data of dynamically generated content, you will most likely have to use Javascript.

    var $elements = $('.tag');
    var elementValues = {};
    
    for (var i = 0; i < $elements; i++)
    {
        elementValues[i] = $elements[i].text();
    }
    
    var sendableData = JSON.stringify(elementValues);
    
    $.post('your_file.php' { data: sendableData }, function () {
        console.log('data sent');
    });
    

    The Javascript will send the data to the PHP file in JSON format. The PHP needs to decode the JSON, and return that data into a PHP object.

    $jsonData = $_POST['data'];
    $data = json_decode($jsonData);
    
    foreach($data as $key => $value)
    {
        $sql = "INSERT INTO table('name') VALUES('$value')";
        $result = mysqli_query($link, $sql);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 CNVcaller安装后无法找到文件
  • ¥15 visual studio2022中文乱码无法解决
  • ¥15 关于华为5g模块mh5000-31接线问题
  • ¥15 keil L6007U报错
  • ¥15 webapi 发布到iis后无法访问
  • ¥15 初学者如何快速上手学习stm32?
  • ¥15 如何自动更换布娃娃图片上的衣服
  • ¥15 心理学eprime编程
  • ¥15 arduino esp8266开发
  • ¥15 stm32单片机通过485发送命令给驱动器控制电机转动,同样的代码f103可以控制电机转动,换到f407不能动了,但是用串口助手调试407显示发送的命令都是正确的,卡了好久了这是发送规则