为了账号安全,请及时绑定邮箱和手机立即绑定

我有一个带有输入作为图像的表单,我不知道如何将输入标签的name属性发送到mysql数据库?

我有一个带有输入作为图像的表单,我不知道如何将输入标签的name属性发送到mysql数据库?

PHP
翻翻过去那场雪 2021-04-07 17:18:13
因此,我希望用户从显示的表单中选择图像之一,但我不想上传图像,只需将该图像的名称发送到我的数据库即可。我看到了一些有关将图像上传到数据库的解决方案,但是我不知道如何仅发送用户选择的图像的名称。最终结果基本上是将信息发送到mysql数据库。<p class="questions">What type of roof do you have?</p><div id="roof-type">  <table>    <tr>      <form method="post" action="action.php">        <input type="image" src="images/asphalt.png" name="asphalt" id="asphalt" />        <input type="image" src="images/metal.png" name="metal" id="metal" />        <input type="image" src="images/flat.png" name="flat" id="flat" />        <input type="image" src="images/clay.png" name="clay" id="clay" />        <input type="image" src="images/cement.png" name="cement" id="cement" />        <input type="image" src="images/other.png" name="other-roof" id="other-roof" />        <input type='hidden' name='roof_type' />       </form>    </tr>    </table></div>  <script>  let input = document.querySelector('input[type="hidden"][name="roof_type"]');let col = document.querySelectorAll('input[type="image"]');Array.prototype.slice.call(col).forEach(img => {    img.addEventListener('click', function(e) {      e.preventDefault();      input.value = this.name;      input.parentNode.submit();    })  })  </script><?php$conn = new mysqli($servername, $username, $password, $dbname);// Check connectionif ($conn->connect_error) {    die("Connection failed: " . $conn->connect_error);}$roof_type = $_POST['roof_type'];$sql = "INSERT INTO testing_database ". "(roof_type) ". "VALUES($roof_type)";mysql_select_db('test');        $retval = mysql_query( $sql, $conn );        if(! $retval ) {           die('Could not enter data: ' . mysql_error());        }        echo "Entered data successfully\n";?>我正在尝试检查是否回显了roof_type,但没有看到任何输出,但是此刻我什么都没有看到,这些是我得到的以下错误:注意:未定义变量:C:\ xampp \ htdocs \ test \ action.php中的链接注意:未定义索引:C:\ xampp \ htdocs \ test \ action.php中的roof_type警告:mysqli_real_escape_string()期望参数1为mysqli, C:\ xampp \ htdocs \ test \ action.php中给出的null
查看完整描述

3 回答

?
哈士奇WWW

TA贡献1799条经验 获得超6个赞

既然您说jQuery是您的一个选择,那么...您可以不使用表单来执行此操作。例子


<img src="images/flat.png" id="flat" class="imgSelect">

然后在jQuery中


$('.imgSelect').click(function(){

  var imgId = $(this).attr('id');

  $.ajax({

  method: "POST",

  url: "databaseProcess.php",

  data: { imgId: imgId }

});

您还可以向每个图像添加“ onclick”事件


<img src="images/flat.png" id="flat" onclick="doFunction()" />


查看完整回答
反对 回复 2021-04-23
  • 3 回答
  • 0 关注
  • 195 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信