我正在做的是在提交表单数据时显示最后插入的数据,表单是多部分/表单数据。我正在使用 jquery 获取此表单数据,在这里我使用 Ajax POST 将此数据发送到 php 文件。在该 php 文件中,我将该数据插入到 db 表中。 Ajax 调用我将该 id 发送到另一个 PHP 文件..使用该 id 我正在显示最后插入的数据...
我的表格是:
<form method="post" enctype="multipart/form-data" name="upload_form" id="data">
<select id="sel">
<option>Select the Project Stream</option>
<option value="1">Computer Science</option>
<option value="2">Mechanical</option>
<option value="3">IT</option>
<option value="4">Web Development</option>
<option value="5">MCA</option>
<option value="6">Civil</option>
</select><br />
<input type="text" id="title" placeholder="Project Title"/><br />
<input type="text" id="vurl" placeholder="If You have any video about project write your video url path here" style="width:435px;"/><br />
<textarea id="prjdesc" name="prjdesc" rows="20" cols="80" style="border-style:groove;box-shadow: 10px 10px 10px 10px #888888;"placeholder="Please describe Your Project"></textarea>
<label for="file">Filename:</label>
<input type="file" name="file" id="file"/><br />
<button>Submit</button>
</form>
我的.js文件:
$("form#data").submit(function() {
alert("update");
var sid=$("#sel").val();
alert(sid);
var ttle = $("#title").val();
alert(ttle);
var text = $("#prjdesc").val();
var vurl = $("#vurl").val();
/*var dataString = 'param='+text+'¶m1='+vurl+'¶m2='+ttle+'¶m3='+id;*/
var formData = new FormData($(this)[0]);
formData.append('param',text);
formData.append('param1',vurl);
formData.append('param2',ttle );
formData.append('param3',sid );
$.ajax({
type:'POST',
data:formData,
url:'insert.php',
success:function(id) {
alert(id);
window.location ="another.php?id="+id;
},
cache: false,
contentType: false,
processData: false
});
return false;
});
插入.php:
<?php
print_r($_FILES);
$desc = $_POST['param'];
echo $desc;
$video = $_POST['param1'];
echo $video ;
$title = $_POST['param2'];
echo $title;
$tech_id=$_POST['param3'];
echo $tech_id;
$host="localhost";
$username="root";
$password="";
$db_name="geny";
$tbl_name="project_details";
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$allowedExts = array("gif", "jpeg", "jpg", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
$url_dir = "C:/wamp/www/WebsiteTemplate4/upload/";
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 50000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
if (file_exists($url_dir . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],$url_dir. $_FILES["file"]["name"]);
// echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
$tmp = "C:/wamp/www/WebsiteTemplate4/upload/" . $_FILES["file"]["name"];
$sql="INSERT INTO $tbl_name (title, content, img_path, video_url, project_tech_Id) VALUES ('$title','$desc','$tmp','$video','$tech_id')";
if(mysql_query($sql)) {
echo mysql_insert_id();
} else {
echo "Cannot Insert";
}
}
}
}
else
{
echo "Invalid file";
}
?>
另一个.php:
<?php
$temp=$_GET['id'];
echo $temp;
$host="localhost";
$username="root";
$password="";
$db_name="geny";
$tbl_name="project_details";
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$query = mysql_query("SELECT content FROM project_details WHERE id=". $temp);
if (!$query)
{
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($query);
echo "<div id='uprjct' style='background:#336699;'>
<p>$row[0]</p>
</div>";
?>
但是我在 insert.php 中返回的 ID 包含元素数组......我不想要所有这些东西,我只想要 ID(这是数字)......请告诉我我的代码有什么问题......