<script type="text/javascript">
function myFunction() {
//var query = '?';
var str = $("form").serialize();
str = str.replace(/\=/g, "='").replace(/\&/g, "'&");
$( "#results" ).text(str+"'" );
var x = $("#results").text();
return x;
}
$('#myButton').on('click',function(){
var jsonString = JSON.stringify(myFunction());
console.log(jsonString);
$.ajax({
url: 'insert_value.php',
data: jsonString,
contentType: 'application/json; charset=utf-8',
type: 'POST',
}).done(function(resp) {
$('#result').html(resp)
});
});
</script>
&#13;
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<?php
include("con_gen.php");
error_reporting(E_ALL);
// DATABASE CONNECTION AND SELECTION VARIABLES - GET THESE FROM YOUR HOSTING COMPANY
$db_host = "localhost"; // PROBABLY THIS IS OK
$db_name = "idcard";
$db_user = "root";
$db_word = "";
// OPEN A CONNECTION TO THE DATA BASE SERVER AND SELECT THE DB
$mysqli = new mysqli($db_host, $db_user, $db_word, $db_name);
// DID THE CONNECT/SELECT WORK OR FAIL?
if ($mysqli->connect_errno)
{
$err
= "CONNECT FAIL: "
. $mysqli->connect_errno
. ' '
. $mysqli->connect_error
;
trigger_error($err, E_USER_ERROR);
}
// RUN A QUERY
$result = mysqli_query($mysqli,"SELECT value FROM combo1");
$num_rows = mysqli_num_rows($result);
//echo "$num_rows Rows\n";
if ($result->num_rows > 0) {
// output data of each row
$array = Array();
$array1 = Array();
while($row = $result->fetch_assoc()) {
//echo "<br> value: ". $row['value']. "<br>";
$array[] = $row['value'];
}
//print_r($array);
$sql = "SELECT static_name FROM static_values";
$result = mysqli_query($mysqli, $sql);
if ($result && mysqli_num_rows($result) > 0)
while($row = mysqli_fetch_array($result)){
//echo "<option>" . $row['static_name'] . "</option>";
$array1[]=$row['static_name'];
//echo $source;
}
// print_r($array1);
foreach ($array as $row)
{
if(in_array($row, $array1))
{
$sql = "SELECT source_table,Alias_name FROM static_values where static_name='$row'";
$res = $mysqli->query($sql);
// DID THE QUERY WORK OR FAIL?
if (!$res)
{
$err
= 'QUERY FAILURE:'
. ' ERRNO: '
. $mysqli->errno
. ' ERROR: '
. $mysqli->error
. ' QUERY: '
. $sql
;
trigger_error($err, E_USER_ERROR);
}
// ARE THERE ANY ROWS IN THE RESULTS SET
if ($res->num_rows == 0)
{
trigger_error("ERROR: NO DATA FOUND BY $sql", E_USER_ERROR);
}
// RETRIEVE THE ROWS INTO AN ARRAY OF HTML STATEMENTS
//$html = "";
echo '<form method="POST" action="" id="myform">';
while ($row = $res->fetch_object())
{
//$html =$html.'
echo'
<tr>
<td>
<div class="row-fluid">
<div class="span3 bgcolor">
<label>'.$row->Alias_name.'</label>
<select id='.$row->source_table.' name='.$row->source_table.' data-live-search="true" class="selectpicker form-control" type="sel">';
/*foreach ( $Data->{$row->source_table} as $key =>$item) {
echo "<option value=".$key.">".$item."</option>" ;
}*/
foreach ( $Data->{$row->source_table} as $key =>$item) {
echo "<option value=".$key.">".$item."</option>";
}
echo '
</select>
</div>
</div>
</td>
</tr>';
}
}
else if($row=="Image")
{
echo '
<tr>
<td>
<div class="row-fluid">
<div class="span3 bgcolor">
<label>'.$row.'</label>
<label>'.$row.'</label>
<input type="file" name="'.$row.'" id="'.$row.'" onchange="document.getElementById(\''.$row.'\').value = this.value">
<input type="text" name="'.$row.'" id="'.$row.'" value="'.$imagepath.'">
</div>
</div>
</td>
</tr>';
}
else
{
//$html =$html.'
echo '
<tr>
<td>
<div class="row-fluid">
<div class="span3 bgcolor">
<label>'.$row.'</label>
<input id='.$row.' type="text" placeholder=" Enter Value " name='.$row.' style="width:100%" class="form-control" required="" />
</select>
</div>
</div>
</td>
</tr>';
}
}
echo'<input id="myButton" type="button" value="SUBMIT" class="btn btn-success" name="submit" onclick="myFunction();"/>';
echo '<p id="results"> </p>';
echo '<pre id="result"></pre></form> ';
//echo '<p><tt id="results"></tt></p>';
}
?>
</body>
</html>
&#13;
<?php else if($row=="Image")
{
echo '<tr>
<td>
<div class="row-fluid">
<div class="span3 bgcolor">
<label>'.$row.'</label>
<input type="file" name='.$row.' id='.$row.' onchange="document.getElementById('.$row.').value = this.value">
<input type="text" name='.$row.' id='.$row.'>
</div>
</div>
</td>
</tr>';
?>
&#13;
这里我在文本框中读取图像完整路径并插入到数据库中,但此处图像路径未进入文本框请告诉我解决方案
我发布了我的整个代码,请让我知道如何将图像路径导入文本框的解决方案
答案 0 :(得分:0)
检查此示例代码并验证您获得的ID是否正确。
lm()
答案 1 :(得分:0)
你需要value属性,每个属性值都应该用单引号或双引号括起来,比如
<?php
echo '<tr>
<td>
<div class="row-fluid">
<div class="span3 bgcolor">
<label>'.$row.'</label>
<input type="file" name="'.$row.'" id="'.$row.'" onchange="document.getElementById(\''.$row.'\').value = this.value">
<input type="text" name="'.$row.'" id="'.$row.'" value="'.$imagepath.'">
</div>
</div>
</td>
</tr>';
?>
答案 2 :(得分:0)
好的,看看代码和你的一条评论似乎$imagepath
在被调用之前没有被声明。您需要声明$imagepath
,然后在调用它之前设置它的值。如果在调用变量之前未声明变量将导致错误,设置变量失败将导致在调用变量时不返回任何内容。