来自输入类型的值=“按钮".没有被添加到数据库 [英] Value from input type="button" isn't being added to database

查看:40
本文介绍了来自输入类型的值=“按钮".没有被添加到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将type="button"表单元素的值添加到mySql数据库时遇到麻烦,并且我想知道是否丢失了某些内容.

I'm having trouble adding the value of type="button" form elements to a mySql database, and I'm wondering if I'm missing something.

编辑-似乎该元素的信息没有从html传递到php,因为它没有回显值.我唯一的问题是这个元素,而表单的其余部分正在正确提交.

Edit - It doesn't look like the information for that element is being passed from the html to the php because it's not echoing a value. My only problem is with this one element and the rest of the form is being submitted properly.

我将其用于在线测验,该测验基于他们选择的图像来构建用户个人资料,并将图像设置为按钮元素的背景图像,而我尝试使用纯HTML(而不是将JavaScript与单选按钮或复选框一起使用).

I'm using this for an online quiz which builds a user profile based upon images they've selected, and am setting the images as background images for the button elements, and I'm trying to do this in straight html (as opposed to using javascript together with radio buttons or check boxes).

<input type="button" name="quiz_start" value="jeans" style="background: url(files/start1.jpg) no-repeat; width:54px;height:140px; cursor:pointer; border:none; color: transparent; font-size : 0">       

我已经简化了php代码,以提出问题(包括指定用户ID并将其限制为一个字段).我还将下面的完整代码包括在内.

I've simplified the php code for purposes of asking the question (including specifying the user id and limiting it to only one field). I've also included the full code below.

<?php
//Start session & connect to database 
$user_id = 3;   

$qry = "INSERT INTO style(user_id, quiz_start) VALUES('$user_id','$_POST[quiz_start]')";
$result = @mysql_query($qry);      
header("location: page2.html");
exit(); 
?>

完整查询为:

The full query is:

$fieldlist=$vallist='';
foreach ($_POST as $key => $value) { 
 $fieldlist.=$key.',';
 $vallist.='\''.($value).'\',';
}
$fieldlist=substr($fieldlist, 0, -1);
$vallist=substr($vallist, 0, -1);
$fieldlist.=', user_id';
$vallist.=','.$user_id;
$setlist='';
foreach ($_POST as $key=>$value){
$setlist.=$key .'=\''.$value.'\',';
}
$setlist=substr($setlist, 0, -1);  
$result = mysql_query('UPDATE style SET '.$setlist.' WHERE user_id='.$user_id);
if (mysql_affected_rows()==0) {
$result = mysql_query('INSERT INTO style ('.$fieldlist.') VALUES ('.$vallist.')');
}  
header("location: page2.html");
exit();
?>

推荐答案

看到您无法回显$ _POST ['quiz_start'],这意味着您的值实际上没有设置.这是因为当您像<input type='button'>中那样使用班级按钮时,您的表单实际上并未像<input type='submit'>

Seeing that you are unable to echo $_POST['quiz_start'] that means your value is not actually set. This is because when you use a class button as in <input type='button'> your form is not actually submitted like <input type='submit'>

一种解决方案是将按钮更改为实际的提交方式和格式,或者...或者您需要通过按钮上的onClick调用javascript函数,如下所示:

One solution would be to change your button to an actual submit and format that... or you need to call a javascript function with an onClick from your button as in:

<input type="button" onClick="myfunction()">

有关我在说什么,请参见此内容发布.

For reference to what I am talking about look at this post.

如果您说其余的表单值都可以提交,但仅按钮值不起作用,则根据您的喜好有几种不同的解决方案.

If as you say the rest of the form values are submitting fine but just the button value is not working you have a few different possible solutions depending on your preference.

  1. 使用选择字段或复选框让人们选择可以用来传递数据的类型.
  2. 使用<input type="button" onClick="myfunction()">在javascript中提交表单,然后在javascript中运行更新查询.
  3. 最后,如果您仍然想在PHP中运行查询,则可以运行javascript函数进行AJAX调用,以返回JSON信息,您可以在页面加载后在其中定义php变量,然后将其插入到您的更新查询.
  1. Use a select field or checkbox for people to select a type in which you can pass your data.
  2. Submit your form in javascript with <input type="button" onClick="myfunction()"> then running your update query in javascript.
  3. Finally if you still want to run your query in PHP you can run a javascript function to make an AJAX call to return JSON information in which you can define a php variable after the page has loaded in which you can then plug into your update query.

这篇关于来自输入类型的值=“按钮".没有被添加到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆