我正在尝试创建一个从mysql获取数据的php页面。我从数据库中获取对象列表,并以不同的形式显示每个对象以编辑值,然后仅保存编辑的值。 但我无法提交表格。并且无法调用json部分和jaascript。
<!DOCTYPE html>
<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Expert Edit</title>
<script src="jquery-1.11.1.min.js"></script>
<script>
$("myforms").submit(function(){
alert("before submit");
var data = {
"tag": "editadsdata",
};
data = $(this).serialize() + "&" + $.param(data);
$.ajax({
type: "POST",
dataType: "json",
url: "http://localhost/expert.php", //Relative or absolute path to response.php file
data: data,
success: function(data) {
alert("Form submitted successfully.");
}
});
return false;
});
</script>
</head>
<body>
<?php
$con=mysqli_connect("localhost","root","","expert");
$result = mysqli_query($con,"select * from ads");
$tmpCount = 1;
while($row = mysqli_fetch_array($result)) {
echo "<form action=\"\" class=\"myforms\" method=\"post\" accept-charset=\"utf-8\">";
echo "<h2>New Entry ! $tmpcount</h2>";
echo "URL : ";
echo '<td><a href="' . $row['url'] . '">[Link]</a></td>';
echo "<input hidden=\"true\" name=\"url\"".$row['url']."\">";
echo "<br>";
echo "IMAGE : ";
echo "<img src=\"".$row['imagepath']."\" />";
echo "<br>";
echo "CATEGORY : ";
echo "<input type=\"text\" name = \"category\" value=\"".$row['category']."\">";
echo "<br>";
echo "SUB-CATEGORY : ";
echo "<input type=\"text\" name = \"subcategory\" value=\"".$row['subcategory']."\">";
echo "<br>";
echo "DESCRIPTION : ";
echo "<input type=\"text\" name = \"description\" value=\"".$row['description']."\">";
echo "<br>";
echo "PRICE RANGE : ";
echo $row['pricerange'];
echo "<br>";
echo "TAG : ";
echo "<input type=\"text\" name = \"tags\" value=\"".$row['tag']."\">";
echo "<br>";
echo "UPLOADED BY : ";
echo $row['uploadedby'];
echo "<br>";
echo "<input type=\"submit\" name=\"Submit\" value=\"submit\" />";
echo "</form>";
$tmpcount++;
}
mysqli_close($con);
?>
</body></html>
检查一下。这是有效的,但这是在HTML中。我想要它的PHP
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>demo</title>
<script type='text/javascript' src='//code.jquery.com/jquery-1.10.1.js'></script>
<link rel="stylesheet" type="text/css" href="/css/result- light.css">
<style type='text/css'>
</style>
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$('.myForms').submit(function () {
alert($(this).attr("id"));
return true;
});
});//]]>
</script>
</head>
<body>
<form action="http://www.google.com" method="get" class="myForms" id="1stform">
<input type="text" value="1st Form" name="q1" />
<input type="submit" value="Submit ALL" />
</form>
<form action="http://www.google.com" method="get" class="myForms" id="2ndform">
<input type="text" value="2nd Form" name="q2" />
<input type="submit" value="Submit ALL" />
</form>
<form action="http://www.google.com" method="get" class="myForms" id="3rdform">
<input type="text" value="3rd Form" name="q3" />
<input type="submit" value="Submit ALL" />
</form>
</body>
</html>
答案 0 :(得分:8)
因为您使用:$("myforms")
。没有像这样的元素。如果myforms是您表单的ID,请使用:
$('#myforms')
如果这是它的类,那么
$('.myforms')
你在ajax中也有一些错误:
var data = {
"tag": "editadsdata",
};
data = $(this).serialize() + "&" + $.param(data);
因为您定义的数据是json,并且您使用字符串覆盖数据,所以它将失败。