您好我有一个wordpress网站,我需要从数据库中获取数据并以表格形式输入。
我有以下代码:
<?php
/*
Plugin Name: Wordpress Plugin Stijn 1
Description: Een Wordpress plugin die data uit een database haalt en invult via Ajax in een form.
Version: 1.1
Author: Stijn Aerts
Author URI: http://stijnaerts.be
License: GPL2
*/
add_action( 'wp_footer', 'ajax_lookup_userdata' );
function ajax_lookup_userdata(){
if (!is_admin()) {
wp_enqueue_script('jquery');
}
?>
<script type="text/javascript">
jQuery(document).ready(function($){
alert('test');
jQuery('#input_1_2').change(function(){
showUser(this.value);
});
function showUser(str){
if(str==''){
jQuery('#input_1_3').val('niets');
}
jQuery.get("getuser.php", { q: str }, function(response){
jQuery('#input_1_3').val(response);
});
}
});
</script>
<?php
}
?>
和getuser.php文件:
<?php
$q = intval($_GET['q']);
$con = mysqli_connect('localhost',****,****,****);
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
$sql="SELECT Naam FROM Personeel WHERE personeelsNummer = '1'";
$result = mysqli_query($con,$sql);
return $result
mysqli_close($con);
?>
当我测试它时,我在文本输入中输入以下输出:
<!DOCTYPE html>
<!--[if IE 7]><html class="ie ie7" lang="nl-NL"><![endif]-->
<!--[if IE 8]><html class="ie ie8" lang="nl-NL"><![endif]-->
<!--[if !(IE 7) | !(IE 8) ]><!-->
<html lang="nl-NL">
<!--<![endif]-->
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<title>Test pagina</title>
<link rel="profile" href="http://gmpg.org/xfn/11">
<link rel="pingback" href="http://test.stijnaerts.be/xmlrpc.php">
<!--[if lt IE 9]> <script src="http://test.stijnaerts.be/wp-content/themes/twentythirteen/js/html5.js"></script> <![endif]-->
<link rel="alternate" type="application/rss+xml" title="Test pagina » Feed" href="http://test.stijnaerts.be/?feed=rss2" />
<link rel="alternate" type="application/rss+xml" title="Test pagina » reacties feed" href="http://test.stijnaerts.be/?feed=comments-rss2" />
<link rel="alternate" type="application/rss+xml" title="Test pagina » Home Reacties feed" href="http://test.stijnaerts.be/?feed=rss2&page_id=2" />
<link rel='stylesheet' id='open-sans-css' href='//fonts.googleapis.com/css?family=Open+Sans%3A300italic%2C400italic%2C600italic%2C300%2C400%2C600&subset=latin%2Clatin-ext&ver=3.9.1' type='text/css' media='all' />
<link rel='stylesheet' id='dashicons-css' href='http://test.stijnaerts.be/wp-includes/css/dashicons.min.css?ver=3.9.1' type='text/css' media='all' />
<link rel='stylesheet' id='admin-bar-css' href='http://test.stijnaerts.be/wp-includes/css/admin-bar.min.css?ver=3.9.1' type='text/css' media='all' />
<link rel='stylesheet' id='twentythirteen-fonts-css' href='//fonts.googleapis.com/css?family=Source+Sans+Pro%3A300%2C400%2C700%2C300italic%2C400italic%2C700italic%7CBitter%3A400%2C700&subset=latin%2Clatin-ext' type='text/css' media='all' />
<link rel='stylesheet' id='genericons-css' href='http://test.stijnaerts.be/wp-content/themes/twentythirteen/fonts/genericons.css?ver=2.09' type='text/css' media='all' />
<link rel='stylesheet' id='twentythirteen-style-css' href='http://test.stijnaerts.be/wp-content/themes/twentythirteen/style.css?ver=2013-07-18' type='text/css' media='all' />
<!--[if lt IE 9]><link rel='stylesheet' id='twentythirteen-ie-css' href='http://test.stijnaerts.be/wp-content/themes/twentythirteen/css/ie.css?ver=2013-07-18' type='text/css' media='all' /><![endif]-->
<script type='text/javascript' src='http://test.stijnaerts.be/wp-includes/js/jquery/jquery.js?ver=1.11.0'></script>
<script type='text/javascript' src='http://test.stijnaerts.be/wp-includes/js/jquery/jquery-migrate.min.js?ver=1.2.1'></script>
<link rel="EditURI" type="application/rsd+xml" title="RSD" href="http://test.stijnaerts.be/xmlrpc.php?rsd" />
<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="http://test.stijnaerts.be/wp-includes/wlwmanifest.xml" />
<link rel='next' title='Contact' href='http://test.stijnaerts.be/?page_id=33' />
<meta name="generator" content="WordPress 3.9.1" />
<link rel='canonical' href='http://test.stijnaerts.be/' />
<link rel='shortlink' href='http://test.stijnaerts.be/' />
<style type="text/css" id="twentythirteen-header-css">
.site-header {
background: url(http://test.stijnaerts.be/wp-content/themes/twentythirteen/images/headers/circle.png) no-repeat scroll top;
background-size: 1600px auto;
}
</style>
<style type="text/css" media="print">
#wpadminbar {
display: none;
}
</style>
<style type="text/css" media="screen">
html {
margin-top: 32px !important;
}
* html body {
margin-top: 32px !important;
}
@media screen and (max-width: 782px) {
html {
margin-top: 46px !important;
}
* html body {
margin-top: 46px !important;
}
}
</style>
</head>
<body class="home page page-id-2 page-template-default logged-in admin-bar no-customize-support single-author">
<div id="page" class="hfeed site">
<header id="masthead" class="site-header" role="banner">
<a class="home-link" href="http://test.stijnaerts.be/" title="Test pagina" rel="home">
<h1 class="site-title">Test pagina</h1>
<h2 class="site-description"></h2>
</a>
<div id="navbar" class="navbar">
<nav id="site-navigation" class="navigation main-navigation" role="navigation">
<h3 class="menu-toggle">Menu</h3> <a class="screen-reader-text skip-link" href="#content" title="Naar de inhoud springen">Naar de inhoud springen</a>
<div class="nav-menu">
<ul>
<li class="page_item page-item-33"><a href="http://test.stijnaerts.be/?page_id=33">Contact</a>
</li>
<li class="page_item page-item-2 current_page_item"><a href="http://test.stijnaerts.be/">Home</a>
</li>
<li class="page_item page-item-41"><a href="http://test.stijnaerts.be/?page_id=41">Registration</a>
</li>
</ul>
</div>
<form role="search" method="get" class="search-form" action="http://test.stijnaerts.be/">
<label> <span class="screen-reader-text">Zoeken naar:</span>
<input type="search" class="search-field" placeholder="Zoeken …" value="" name="s" title="Zoeken naar:" />
</label>
<input type="submit" class="search-submit" value="Zoeken" />
</form>
</nav>
<!-- #site-navigation -->
</div>
<!-- #navbar -->
</header>
<!-- #masthead -->
<div id="main" class="site-main">
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<article id="post-2" class="post-2 page type-page status-publish hentry">
<header class="entry-header">
<h1 class="entry-title">Home</h1>
</header>
<!-- .entry-header -->
<div class="entry-content">
<p> </p>
<p> </p>
</div>
<!-- .entry-content -->
<footer class="entry-meta"> <span class="edit-link"><a class="post-edit-link" href="http://test.stijnaerts.be/wp-admin/post.php?post=2&action=edit">Bewerken</a></span>
</footer>
<!-- .entry-meta -->
</article>
<!-- #post -->
<div id="comments" class="comments-area">
<div id="respond" class="comment-respond">
<h3 id="reply-title" class="comment-reply-title">Geef een reactie <small><a rel="nofollow" id="cancel-comment-reply-link" href="/getuser.php/?q=h#respond" style="display:none;">Reactie annuleren</a></small></h3>
<form action="http://test.stijnaerts.be/wp-comments-post.php" method="post" id="commentform" class="comment-form" novalidate>
<p class="logged-in-as">Ingelogd als <a href="http://test.stijnaerts.be/wp-admin/profile.php">admin</a>. <a href="http://test.stijnaerts.be/wp-login.php?action=logout&redirect_to=http%3A%2F%2Ftest.stijnaerts.be%2F&_wpnonce=8100200395" title="Dit account uitloggen">Uitloggen?</a>
</p>
<p class="comment-form-comment">
<label for="comment">Reactie</label>
<textarea id="comment" name="comment" cols="45" rows="8" aria-required="true"></textarea>
</p>
<p class="form-allowed-tags">De volgende <abbr title="HyperText Markup Language">HTML</abbr>-tags en -attributen zijn toegestaan: <code><a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> </code>
</p>
<p class="form-submit">
<input name="submit" type="submit" id="submit" value="Reactie plaatsen" />
<input type='hidden' name='comment_post_ID' value='2' id='comment_post_ID' />
<input type='hidden' name='comment_parent' id='comment_parent' value='0' />
</p>
<input type="hidden" id="_wp_unfiltered_html_comment_disabled" name="_wp_unfiltered_html_comment_disabled" value="ff816f89c6" />
<script>
(function() {
if (window === window.parent) {
document.getElementById('_wp_unfiltered_html_comment_disabled').name = '_wp_unfiltered_html_comment';
}
})();
</script>
</form>
</div>
<!-- #respond -->
</div>
<!-- #comments -->
</div>
<!-- #content -->
</div>
<!-- #primary -->
</div>
<!-- #main -->
<footer id="colophon" class="site-footer" role="contentinfo">
<div id="secondary" class="sidebar-container" role="complementary">
<div class="widget-area">
<aside id="search-2" class="widget widget_search">
<form role="search" method="get" class="search-form" action="http://test.stijnaerts.be/">
<label> <span class="screen-reader-text">Zoeken naar:</span>
<input type="search" class="search-field" placeholder="Zoeken …" value="" name="s" title="Zoeken naar:" />
</label>
<input type="submit" class="search-submit" value="Zoeken" />
</form>
</aside>
</div>
<!-- .widget-area -->
</div>
<!-- #secondary -->
<div class="site-info"> <a href="http://wordpress.org/" title="Semantisch persoonlijk publicatie platform">Ondersteund door WordPress</a>
</div>
<!-- .site-info -->
</footer>
<!-- #colophon -->
</div>
<!-- #page -->
<script type="text/javascript">
jQuery(document).ready(function($) {
alert('test');
jQuery('#input_1_2').change(function() {
showUser(this.value);
});
function showUser(str) {
if (str == '') {
jQuery('#input_1_3').val('niets');
}
jQuery.get("getuser.php", {
q: str
}, function(response) {
jQuery('#input_1_3').val(response);
});
}
});
</script>
<script type='text/javascript' src='http://test.stijnaerts.be/wp-includes/js/admin-bar.min.js?ver=3.9.1'></script>
<script type='text/javascript' src='http://test.stijnaerts.be/wp-includes/js/comment-reply.min.js?ver=3.9.1'></script>
<script type='text/javascript' src='http://test.stijnaerts.be/wp-includes/js/masonry.min.js?ver=3.1.2'></script>
<script type='text/javascript' src='http://test.stijnaerts.be/wp-includes/js/jquery/jquery.masonry.min.js?ver=3.1.2'></script>
<script type='text/javascript' src='http://test.stijnaerts.be/wp-content/themes/twentythirteen/js/functions.js?ver=2014-03-18'></script>
<script type="text/javascript">
(function() {
var request, b = document.body,
c = 'className',
cs = 'customize-support',
rcs = new RegExp('(^|\\s+)(no-)?' + cs + '(\\s+|$)');
request = true;
b[c] = b[c].replace(rcs, ' ');
b[c] += (window.postMessage && request ? ' ' : ' no-') + cs;
}());
</script>
<div id="wpadminbar" class="nojq nojs" role="navigation"> <a class="screen-reader-shortcut" href="#wp-toolbar" tabindex="1">Spring naar de toolbar</a>
<div class="quicklinks" id="wp-toolbar" role="navigation" aria-label="Topnavigatiebalk." tabindex="0">
<ul id="wp-admin-bar-root-default" class="ab-top-menu">
<li id="wp-admin-bar-wp-logo" class="menupop"><a class="ab-item" aria-haspopup="true" href="http://test.stijnaerts.be/wp-admin/about.php" title="Over WordPress"><span class="ab-icon"></span></a>
<div class="ab-sub-wrapper">
<ul id="wp-admin-bar-wp-logo-default" class="ab-submenu">
<li id="wp-admin-bar-about"><a class="ab-item" href="http://test.stijnaerts.be/wp-admin/about.php">Over WordPress</a>
</li>
</ul>
<ul id="wp-admin-bar-wp-logo-external" class="ab-sub-secondary ab-submenu">
<li id="wp-admin-bar-wporg"><a class="ab-item" href="https://nl.wordpress.org/">WordPress.org</a>
</li>
<li id="wp-admin-bar-documentation"><a class="ab-item" href="http://codex.wordpress.org/">Documentatie</a>
</li>
<li id="wp-admin-bar-support-forums"><a class="ab-item" href="https://nl.forums.wordpress.org/">Ondersteuningsforums</a>
</li>
<li id="wp-admin-bar-feedback"><a class="ab-item" href="https://nl.forums.wordpress.org/">Terugkoppeling</a>
</li>
</ul>
</div>
</li>
<li id="wp-admin-bar-site-name" class="menupop"><a class="ab-item" aria-haspopup="true" href="http://test.stijnaerts.be/wp-admin/">Test pagina</a>
<div class="ab-sub-wrapper">
<ul id="wp-admin-bar-site-name-default" class="ab-submenu">
<li id="wp-admin-bar-dashboard"><a class="ab-item" href="http://test.stijnaerts.be/wp-admin/">Dashboard</a>
</li>
</ul>
<ul id="wp-admin-bar-appearance" class="ab-submenu">
<li id="wp-admin-bar-themes"><a class="ab-item" href="http://test.stijnaerts.be/wp-admin/themes.php">Thema's</a>
</li>
<li id="wp-admin-bar-customize" class="hide-if-no-customize"><a class="ab-item" href="http://test.stijnaerts.be/wp-admin/customize.php?url=http%3A%2F%2Ftest.stijnaerts.be%2Fgetuser.php%2F%3Fq%3Dh">Aanpassen</a>
</li>
<li id="wp-admin-bar-widgets"><a class="ab-item" href="http://test.stijnaerts.be/wp-admin/widgets.php">Widgets</a>
</li>
<li id="wp-admin-bar-menus"><a class="ab-item" href="http://test.stijnaerts.be/wp-admin/nav-menus.php">Menu's</a>
</li>
<li id="wp-admin-bar-header"><a class="ab-item" href="http://test.stijnaerts.be/wp-admin/themes.php?page=custom-header">Header</a>
</li>
</ul>
</div>
</li>
<li id="wp-admin-bar-updates"><a class="ab-item" href="http://test.stijnaerts.be/wp-admin/update-core.php" title="3 Plugins bijwerken, Vertalingsupdates"><span class="ab-icon"></span><span class="ab-label">4</span><span class="screen-reader-text">3 Plugins bijwerken, Vertalingsupdates</span></a>
</li>
<li id="wp-admin-bar-comments"><a class="ab-item" href="http://test.stijnaerts.be/wp-admin/edit-comments.php" title="0 reacties wachten op moderatie"><span class="ab-icon"></span><span id="ab-awaiting-mod" class="ab-label awaiting-mod pending-count count-0">0</span></a>
</li>
<li id="wp-admin-bar-new-content" class="menupop"><a class="ab-item" aria-haspopup="true" href="http://test.stijnaerts.be/wp-admin/post-new.php" title="Toevoegen"><span class="ab-icon"></span><span class="ab-label">Nieuw</span></a>
<div class="ab-sub-wrapper">
<ul id="wp-admin-bar-new-content-default" class="ab-submenu">
<li id="wp-admin-bar-new-post"><a class="ab-item" href="http://test.stijnaerts.be/wp-admin/post-new.php">Bericht</a>
</li>
<li id="wp-admin-bar-new-media"><a class="ab-item" href="http://test.stijnaerts.be/wp-admin/media-new.php">Media</a>
</li>
<li id="wp-admin-bar-new-link"><a class="ab-item" href="http://test.stijnaerts.be/wp-admin/link-add.php">Link</a>
</li>
<li id="wp-admin-bar-new-page"><a class="ab-item" href="http://test.stijnaerts.be/wp-admin/post-new.php?post_type=page">Pagina</a>
</li>
<li id="wp-admin-bar-new-user"><a class="ab-item" href="http://test.stijnaerts.be/wp-admin/user-new.php">Gebruiker</a>
</li>
</ul>
</div>
</li>
<li id="wp-admin-bar-edit"><a class="ab-item" href="http://test.stijnaerts.be/wp-admin/post.php?post=2&action=edit">Pagina bewerken</a>
</li>
</ul>
<ul id="wp-admin-bar-top-secondary" class="ab-top-secondary ab-top-menu">
<li id="wp-admin-bar-search" class="admin-bar-search">
<div class="ab-item ab-empty-item" tabindex="-1">
<form action="http://test.stijnaerts.be/" method="get" id="adminbarsearch">
<input class="adminbar-input" name="s" id="adminbar-search" type="text" value="" maxlength="150" />
<input type="submit" class="adminbar-button" value="Zoeken" />
</form>
</div>
</li>
<li id="wp-admin-bar-my-account" class="menupop with-avatar"><a class="ab-item" aria-haspopup="true" href="http://test.stijnaerts.be/wp-admin/profile.php" title="Mijn account">Hallo, admin<img alt='' src='http://1.gravatar.com/avatar/36a6b3e27a8b5a0a65cc9ce246ca0256?s=26&d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D26&r=G' class='avatar avatar-26 photo' height='26' width='26' /></a>
<div class="ab-sub-wrapper">
<ul id="wp-admin-bar-user-actions" class="ab-submenu">
<li id="wp-admin-bar-user-info">
<a class="ab-item" tabindex="-1" href="http://test.stijnaerts.be/wp-admin/profile.php">
<img alt='' src='http://1.gravatar.com/avatar/36a6b3e27a8b5a0a65cc9ce246ca0256?s=64&d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D64&r=G' class='avatar avatar-64 photo' height='64' width='64' /><span class='display-name'>admin</span><span class='username'>stijnxk59</span>
</a>
</li>
<li id="wp-admin-bar-edit-profile"><a class="ab-item" href="http://test.stijnaerts.be/wp-admin/profile.php">Mijn profiel wijzigen</a>
</li>
<li id="wp-admin-bar-logout"><a class="ab-item" href="http://test.stijnaerts.be/wp-login.php?action=logout&_wpnonce=8100200395">Uitloggen</a>
</li>
</ul>
</div>
</li>
</ul>
</div> <a class="screen-reader-shortcut" href="http://test.stijnaerts.be/wp-login.php?action=logout&_wpnonce=8100200395">Uitloggen</a>
</div>
</body>
</html>
这是我第一次做这样的事情,特别是在Wordpress中,我不知道造成这种情况的原因。我只想让脚本工作!洛尔。
非常感谢。
答案 0 :(得分:0)
我要做的第一件事是使用浏览器网络检查器来验证$ .get()调用正在请求的URL。确保URL正确,并将其直接加载到自己的浏览器选项卡中以解决getuser.php文件问题。我的猜测是URL不正确,你得到一个服务器或WordPress 404页面,因此你的响应中的HTML。在您的javascript中使用完整的URL可能是明智之举,因为相当的永久链接可能会导致相对URL(例如“getuser.php”)出现问题。更好的是,通过WordPress的“admin-ajax.php”端点路由您的ajax请求,因为它更安全(这些请求永远不会被插件缓存等),并使用WordPress操作来响应请求。
然后,正如Kevin指出的那样,getuser.php文件没有向浏览器发送任何信息。您需要打印或回显某些内容以允许javascript响应以执行任何操作。最简单的方法是使用数组格式获取数据库信息,然后使用json_encode($ array_of_data)将其发送到编码为JSON的浏览器。然后,您的JavaScript代码可以轻松地将其用作简单的JavaScript对象。