我想通过在选择选项中选择数据库在文本编辑器中执行查询。
diendifier.php是我选择的所有条件的页面:if($ database ==" data1"){}
这是我的代码:
<div class="form-group">
<label>Editor : </label>
<textarea id = "editor" name = "editor" class="form-control" rows="10" placeholder="Enter ..."></textarea>
</div>
<div class="col-md-2">
<div class="form-group">
<select name="database" id="database" class="form-control select2" style="width: 100%;">
<option value="data1">data1</option>
<option value="data2">data2</option>
<option value="data3">data3</option>
</select>
</div>
</div>
<button type="submit" name="submit" class="btn btn-primary" onclick="executor();">Execute</button>
<h3 id = "results">Results</h3>
<script>
var editor = editor.edit("editor");
</script>
<script>
function executor()
{
document.getElementById('cover').style.display = "block";
contentsql = editor.getValue() ;
dbvalue = document.getElementById("database").value;
$.post( "identifier.php", { query: contentsql , database: dbvalue })
.done(function( data ) {
document.getElementById("results").innerHTML = data ;
$('#cover').fadeOut(500);
});
}
</script>
页面的代码idnetifier.php:
require_once ('../../config/connection.php');
function array2table($array, $recursive = false, $null = ' ')
{
if (empty($array) || !is_array($array)) { return false;}
if (!isset($array[0]) || !is_array($array[0])) { $array = array($array);}
$table = " <div id='' class='pull-right'>
<form action='/module/getCSV.php' method='post'>
<input type='hidden' name='csv_text' id='csv_text'>
<input type='submit' value='Export as CSV' onclick='getCSVData()' class='btn btn-success btn-xs'>
</form>
</div>
<table class='table datatable' id='table_without_sorting' >n";
// The header
$table .= "t<tr>";
// Take the keys from the first row as the headings
foreach (array_keys($array[0]) as $heading) {
$table .= '<th>' . $heading . '</th>';
}
$table .= "</tr>n";
// The body
foreach ($array as $row) {
$table .= "t<tr>" ;
foreach ($row as $cell) {
$table .= '<td>';
$kounter++;
// Cast objects
if (is_object($cell)) { $cell = (array) $cell; }
if ($recursive === true && is_array($cell) && !empty($cell)) {// Recursive mode $table .= "n" . array2table($cell, true, true) . "n";
} else { $table .= (strlen($cell) > 0) ?htmlspecialchars((string) $cell) :$null;}
$table .= '</td>';
}
$table .= "</tr>n";
}
$table .= '</table>';
return $table;
}
$query = $_POST['query'];
$database = $_POST['database'];
$sql = $query ;
if($database=="data1"){
$request = $co_data1 -> prepare($sql);
$request -> execute();
$data_request = $request -> fetchAll(PDO::FETCH_ASSOC);
}
if($database=="data2"){
$request = $co_data2 -> prepare($sql);
$request -> execute();
$data_rreq = $request -> fetchAll(PDO::FETCH_ASSOC);
}
if($database=="data3"){
$request = $co_data3 -> prepare($sql);
$request -> execute();
$data_request = $request -> fetchAll(PDO::FETCH_ASSOC);
}
echo array2table($data_request);
echo "<hr>";
echo "<b>".$kounter."Database: ".$datbase."</b>";
?>
谢谢。
所以,您有Muliple问题。
我假设您的提交/执行程序按钮在具有操作属性的表单标签中。在这种情况下,在单击按钮后,将提交表单,并且JavaScript函数(executor)没有机会完全执行,因为您正在重定向。
如果这是真的,则必须防止此作品的默认操作。
function executor(event) {
event.preventDefault();
...
}
和按钮:
<btn... onclick="executor(event)" ...>...</btn>
您与editor.edit("编辑器")的目标是什么?那是从图书馆那里吗?编辑器显然是未定义的,因为直到目前您没有编辑器变量。