i'm working on a wordpress site using buddypress. So, i'm making a page where you can see members and search for them using a database: wp_users.
This works fine if i have it as a php when adding it to a wp page it shows up correctly but when clicking submit I get a 404 error.
This is the code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Search Contacts</title>
<style type="text/css">
#spacer {
border-top-width: 1px;
border-right-width: 1px;
border-bottom-width: 1px;
border-left-width: 1px;
border-top-style: solid;
}
</style>
</head>
<p><body>
<p> </p>
<form method="post" action="" id="searchform">
<p>
<input name="name" type="text">
<input type="submit" name="submit" value="Søk">
</p>
<p>Tips: du kan søke etter adresse, navn og e-post.</p>
</form>
<table width="600">
<tr>
<th width="200" height="50">Navn</th>
<th width="200"> </th>
<th width="200">E-post adresse</th>
</tr>
<?php
/* Change next two lines if using online*/
$db="skynnaks_skyn";
$link = mysql_connect('localhost', 'skynnaks_skyn', '*password*');
if (! $link) die(mysql_error());
mysql_select_db($db , $link) or die("Couldn't open $db: ".mysql_error());
$result = mysql_query( "SELECT * FROM wp_users" )
or die("SELECT Error: ".mysql_error());
$num_rows = mysql_num_rows($result);
if ($result) {
while ($row = mysql_fetch_array($result)) {
$_SESSION['id']=$row['id'];
#echo "<tr><a href='#'>".$row['display_name']."</a>";
echo "<td><a href="."/members/".$row['user_nicename']."/profile/>".$row['display_name']."</a></td>";
#echo "<a href=" '.$row['id'].'>Bruker: ".$row['user_url']."</a>";
echo "<td>".$row['user_url']."</td>";
echo "<td>".$row['user_email']."</td></tr>";
}
}
?>
</table>
<div id="spacer">
<?php
if(isset($_POST['submit'])){
if(preg_match("/^[ a-zA-Z]+/", $_POST['name'])){
$name=$_POST['name'];
//connect to the database
$db=mysql_connect ("localhost", "skynnaks_skyn", "*password*") or die ('I cannot connect to the database because: ' . mysql_error());
//-select the database to use
$mydb=mysql_select_db("skynnaks_skyn");
//-query the database table
$sql="SELECT ID, user_login, user_nicename FROM wp_users WHERE user_login LIKE '%" . $name . "%' OR user_nicename LIKE '%" . $name ."%'";
//-run the query against the mysql query function
$result=mysql_query($sql);
//-create while loop and loop through result set
while($row=mysql_fetch_array($result)){
$loginName =$row['user_login'];
$niceName=$row['user_nicename'];
$ID=$row['ID'];
//-display the result of the array
echo "<ul>\n";
echo "<li>" . "<a href="."/members/".$loginName."/profile/>".$niceName."</a>" ;
echo "</ul>";
}
}
else{
echo "<p>Please enter a search query</p>";
}
}
?>
</div>
<p> </p>
</body>
</html>
</p>
PS: I cant use default permalinks due buddypress plugin. And i know that buddypress comes with a members search, but i have installed norwegian language pack, but it wont work with members search in buddypress.
Thanks for the help :)
It looks like this problem,
so change your input tag name with any specific name attribute.
for example,
<input name="membername" type="text" />
but don't forget to get variable with same from $_POST
Related
I am just trying to figure out the logic behind inserting form data into the database. Below code (load.php) works fine. Howewer I receive somehow a syntax error in process.php, besides I'm not sure if my insert code is correct. I also doubt this code is secure, what is the key security factor that I should consider while working with databases? I know I ask many questions but I just try to get the whole picture. I would appreciate any advice and thoughts.
thanks!
**//process.php**
<?php
require ("load.php");
$fname= $_POST['fname'];
$lname= $_POST['lname'];
$sql = "INSERT INTO registration (firstname, lastname) VALUES ('$_POST[fname]','$_POST[lname]')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
**//load.php**
<?php
$servername = "localhost";
$database = "registration";
$username = "root";
$password = "";
$conn = mysqli_connect($servername, $username, $password, $database);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
**//index.php**
<?php require ("load.php"); ?>
<html>
<head>
<title>Registration Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<h3>Registration Form</h3>
<form name="registration" method="post" action="process.php">
<table border="0" cellspacing="2" cellpadding="2">
<tr><td>First Name:</td><td><input type="text" name="fname"></td></tr>
<tr><td>Last Name:</td><td><input type="text" name="lname"></td></tr>
<tr><td> </td><td><input type="submit" name="submit" value="Register"></td></tr>
</table>
</form>
</body>
</html>
Try following code for process.php
This will works
require ("load.php");
$stmt = $conn->prepare("INSERT INTO MyGuests (firstname, lastname) VALUES (?, ?)");
$stmt->bind_param("ss", $firstname, $lastname);
$fname= $_POST['fname'];
$lname= $_POST['lname'];
if ($stmt->execute()) {
echo "New record created successfully";
}
$stmt->close();
I am having a problem, i can connect to my database but it wont save in database what i wrote in a text area. Every time i click update it saves as blank, if i write lalala in mysql directly and refresh the page where text area is i can see lalala in text area but if i replace the text lalala in text area for something else and click update, it deletes the previous text lalala and leaves the field blank. Heres my code:
----------------------------- File 1 ---------------------------------
<?
include("header.inc.php");
$result5 = mysql_query("SELECT faq FROM `demo_a_faq`");
$myrow5 = mysql_fetch_row($result5);
$faq = $myrow5[0];
?>
<?
include("../templates/admin-header.txt");
?>
<form method="post" action="faq2.php">
<TABLE bgcolor="#FFFFFF" bordercolor="#000008" border="0" width="95%" align="center">
<TR>
<TD width="50%"><center><font face="Verdana, Arial, Helvetica, sans-serif" size="2"> <b>Edit FAQ:</b><br><textarea name="faneu" type="text" cols="80" rows="25"><? echo "$faq"; ?></textarea></TD>
</TR>
</TABLE><br><br>
<center><input type="submit" value="Update"></form></center>
<?
include("../templates/admin-footer.txt");
?>
----------------------------- File 2 ---------------------------------
<?
include("header.inc.php");
$asl = "UPDATE `demo_a_faq` SET `faq` = '$faneu'";
$results = mysql_query($asl) or die(mysql_error());
?>
<?
include("../templates/admin-header.txt");
?>
<center><br><br><br><b>Updated!</b></center>
<?
include("../templates/admin-footer.txt");
?>
In header.inc.php i simply have the conection to database.
Can someone please tell me why it is not saving what i write in text area to database as it is driving me crazy.
Thanks in advance
Try this:
<?php
include("header.inc.php");
// Always escape variables used in SQL-queries to avoid SQL-injections.
$faneu = mysql_real_escape_string($_POST['faneu']);
$asl = "UPDATE `demo_a_faq` SET `faq` = '$faneu'";
$results = mysql_query($asl) or die(mysql_error());
?>
<?
include("../templates/admin-header.txt");
?>
<center><br><br><br><b>Updated!</b></center>
<?
include("../templates/admin-footer.txt");
?>
You don't POST your textarea value to "FILES 2".Be carefull.
I have PHP coding that works but I have had no luck transferring this to a HTML form. Any advise?
<?php
$db_host = "localhost";
$db_username = "combsb_combsb";
$db_pass = "pat60086";
$db_name = "combsb_sample";
#mysql_connect ("$db_host", "$db_username", "$db_pass") or die ("Could not connect to MySQL");
#mysql_select_db("$db_name") or die ("No Database");
Echo"Successful Connection";
$sql = "SELECT compname FROM Crew";
$result = mysql_query($sql);
echo "<select name='compname'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['compname'] . "'>" . $row['compname'] . "</option>";
}
echo "</select>";
?>
First I'd make sure to add the HTML boilerplate around your PHP. Then I think you mean mysql_fetch_row instead of mysql_fetch_array. Your final file should look something like:
<!DOCTYPE html>
<html>
<head></head>
<body>
<?php
// *Cut out for brevity, remember to paste back in*
$result = mysql_query($sql);
?>
<form action="" method="POST">
<?php
echo "<select name='compname'>";
while ($row = mysql_fetch_row($result)) {
echo "<option value='" . $row['compname'] . "'>" . $row['compname'] . "</option>";
}
echo "</select>";
?>
</form>
</body>
</html>
Post your output / errors next time or if this doesn't work
I am trying to create a page in my WordPress Admin Area that displays excerpts of posts and various custom field meta in a table-style layout.
If this were a front-end WordPress Template, I could do this very easily using a WordPress Loop and Query, however, I am not so sure how I would go about doing this on a page in the admin area.
Would it be the same, or would I need to use a completely new method? If so, could someone please provide a working example of how I would do this?
The admin page will be created using an included file within my functions.php - or at least that is the plan at the moment, so I just need help in figuring out how to pull the WordPress Excerpts and Post Meta.
you can use the WP_Query object everytime after WordPress is initialized, so if you like you can even make thousands of nested queries in den WordPress backend if you want to do this.
This is the way to go:
Create an action to add your backend page - write a Plugin or put it into your functions.php
Setup the Menu Page - the code is an example for a full backend administration Page of your Theme
Include your queries using the WP_Query object - optionally make database queries directly (http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query). Possibly use the "widefat" class of WordPress, for pretty formatting.
Make sure that your changes are saved correctly
add_action('admin_menu', 'cis_create_menu');
function cis_create_menu() {
//create new top-level menu
add_menu_page(__('Theme Settings Page',TEXTDOMAIN),__('Configure Theme',TEXTDOMAIN), 'administrator', __FILE__, 'cis_settings_page', '');
//call register settings function
add_action('admin_init','cis_register_settings');
}
function cis_register_settings() {
register_setting('cis-settings-group','cis_options_1','cis_validate_settings');
}
function cis_settings_page() {
// All Text field settings
$op_fields = array(
array(__('Label 1','textdomain'),"Description 1")
);
?>
<div class="wrap">
<h2><?php echo THEME_NAME; _e(": Settings",TEXTDOMAIN); ?></h2>
<?php
settings_errors();
?>
<form method="post" action="options.php">
<?php
settings_fields( 'cis-settings-group' );
$options = get_option('cis_options_1');
?>
<h3><?php _e('General','textdomain'); ?></h3>
<table class="widefat">
<thead>
<tr valign="top">
<th scope="row"><?php _e('Setting','ultrasimpleshop'); ?></th>
<th scope="row"><?php _e('Value','ultrasimpleshop'); ?></th>
<th scope="row"><?php _e('Description','ultrasimpleshop'); ?></th>
<th scope="row"><?php _e('ID','ultrasimpleshop'); ?></th>
</tr>
</thead>
<tbody>
<?php
// the text-settings we define fast display
$i=1;
foreach($op_fields as $op) {?>
<tr valign="top">
<td><label for="cis_oset_<?php echo $i; ?>"><?php echo $op[0]; ?></label></td>
<td><input size="100" id="cis_oset_<?php echo $i; ?>" name="cis_options_1[cis_oset_<?php echo $i; ?>]" type="text" value="<?php echo esc_attr($options['cis_oset_'.$i]);?>" /></td>
<td class="description"><?php echo $op[1]; ?></td>
<td class="description"><?php echo $i; ?></td>
</tr>
<?php
$i++;
} ?>
</tbody>
</table>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e('Save Changes',TEXTDOMAIN) ?>" />
</p>
</form>
</div>
<?php }
// Validate the user input - if nothing to validate, just return
function cis_validate_settings( $input ) {
$valid = array();
$i= 1;
while(isset($input['cis_oset_'.$i])) {
$valid['cis_oset_'.$i] = $input['cis_oset_'.$i];
$i++;
}
$cis_additional_settings = get_option('cis_options_1');
foreach($input as $ikey => $ivalue) {
if($ivalue != $valid[$ikey]) {
add_settings_error(
$ikey, // setting title
"cis_oset_".$ikey, // error ID
str_replace("%s",$ikey,__('Invalid Setting in Settings Area ("%s"). The value was not changed.',TEXTDOMAIN)), // error message
'error' // type of message
);
$valid[$ikey] = $cis_additional_settings[$ikey];
}
}
return $valid;
}
outside the loop you would need to use
$post->post_excerpt
or try this
function get_the_excerpt_here($post_id)
{
global $wpdb;
$query = "SELECT post_excerpt FROM $wpdb->posts WHERE ID = $post_id LIMIT 1";
$result = $wpdb->get_results($query, ARRAY_A);
return $result[0]['post_excerpt'];
}
Actually I have a CGI form which consists of textfields and I need a combobox in which I can enter my own data dynamically. May be it seems very silly question but I am new to cgi-perl as well as HTML so no idea what to do. Here is my form:
#!C:\perl\bin\perl.exe
use CGI;
use CGI qw/:standard/;
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
my $q = new CGI;
use DBI;
use CGI qw(:all);
use strict;
use warnings;
print "Content-Type: text/html\n\n";
print $q->header ( );
if ( $q->param("submit") )
{
process_form ( );
}
else
{
display_form ( );
}
sub process_form
{
if ( validate_form ( ) )
{
display_form ( );
}
}
sub validate_form
{
my $User_Name = $q->param("User_Name");
my $User_Password= $q->param("User_Password");
my $User_Permission = $q->param("User_Permission");
my $User_Department= join(", ",$q->param("User_Department"));
my $error_message = "";
$error_message .= "Please enter your name<br/>" if( !$User_Name );
$error_message .= "Please enter your Password<br/>" if( ! $User_Password );
$error_message .= "Please Select a permission<br/>" if( !$User_Permission );
$error_message .= "Please select atleast 1 department<br/>" if(!$User_Department);
if ( $error_message )
{
display_form (
$error_message,$User_Name,$User_Password,$User_Permission,$User_Department);
return 0;
}
else
{
my $dbh = DBI->connect("dbi:SQLite:DEVICE.db","", "",{RaiseError => 1, AutoCommit =>
1 } );
my $sql = "SELECT COUNT(UserName) FROM UsersList WHERE UserName='$User_Name'";
my $sth = $dbh->prepare($sql) or die("\n\nPREPARE ERROR:\n\n$DBI::errstr");
$sth->execute or die("\n\nQUERY ERROR:\n\n$DBI::errstr");
my ($n) = $dbh->selectrow_array($sth);
$sth->finish();
if ($n > 0) {
print "Record Already Exists";
}
else {
my $sql = "INSERT INTO UsersList (UserName,Password,Permission,Department) VALUES
('$User_Name ',' $User_Password','$User_Permission','$User_Department')";
my $sth = $dbh->prepare($sql);
$sth->execute;
print "Record Added Successfully";
$sth->finish();
$dbh->commit or die $dbh->errstr;
}
$dbh->disconnect;
}
}
sub display_form
{
my $error_message = shift;
my $User_Name = shift;
my $User_Password = shift;
my $User_Permission= shift;
my $User_Department= shift;
my $User_Permission_Add_sel = $User_Permission eq "Add" ? " checked" : "";
my $User_Permission_Edit_sel =$User_Permission eq "Edit" ? " checked" : "";
my $User_Permission_Delete_sel =$User_Permission eq "Delete" ? " checked" : "";
my $User_Permission_View_sel =$User_Permission eq "View" ? " checked" : "";
my $User_Department_html = "";
my $dbh = DBI->connect("dbi:SQLite:DEVICE.db","", "",{RaiseError => 1, AutoCommit =>
1 } );
my $sql = "select DepartmentName from Departments order by DepartmentName";
my $sth = $dbh->prepare($sql);
$sth->execute() ;
while (my $User_Department_option= $sth->fetchrow_array)
{
$User_Department_html.= "<option value=\"$User_Department_option\"";
$User_Department_html.= " selected" if ( $User_Department_option eq
$User_Department );
$User_Department_html.= ">$User_Department_option</option>";
}
$sth->finish();
$dbh->commit or die $dbh->errstr;
print <<END_HTML;
<html>
<head><title>Form Validation</title></head>
<body>
<form action="AddUser.cgi" method="post">
<input type="hidden" name="submit" value="Submit">
<p>$error_message</p>
<TABLE BORDER="1" align="center">
<TR>
<TD>Name</TD>
<TD> <input type="text" name="User_Name" value="$User_Name"></TD>
</TR>
<TR>
<TD>Password</TD>
<TD colspan="2"><input type="password" name="User_Password" value="$User_Password"
size="20" maxlength="15" /></TD>
</TR>
<TR>
<TD>Role</TD>
<TD>"HERE I NEED A COMBOBOX"</TD>
</TR>
<TR>
<TD>Permission</TD>
<TD><input type="radio" name="User_Permission"
value="Add"$User_Permission_Add_sel>Add<input type="radio" name="User_Permission"
value="Edit"$User_Permission_Edit_sel>Edit<input type="radio"
name="User_Permission" value="Delete"$User_Permission_Delete_sel>Delete<input
type="radio" name="User_Permission" value="View"$User_Permission_View_sel>View</TD>
</TR>
<TR>
<TD>Department</TD>
<TD colspan="2"> <select name="User_Department" MULTIPLE
SIZE=4>$User_Department_html</select></TD>
</TR>
</TR>
<TR>
<TD align="center" colspan="2">
<input type="submit" name="submit" value="ADD">
</TD>
</TR>
</TABLE
</form>
</body></html>
END_HTML
}
What you're looking for here isn't done on the Perl side, but on the HTML+Javascript side. As noted by others, HTML does not have a built-in combo box form element. So, you're stuck with Javascript.
Personally, I like using JQuery whenever working with Javascript. It's a Javascript library which makes manipulating web pages elements much easier.
Specific to your question, you'll want to look at http://jqueryui.com/demos/autocomplete/ (there is an actual combobox demo linked on the right, if you really, really need a combobox instead of a Google-style autocomplete text field.
Not related to the combobox, but you might also want to look at Template::Toolkit - a templating system for Perl (and others) that will allow you to take the HTML out of your perl scripts. Believe me, having the HTML embedded in CGI scripts for anything beyond the most basic usages will turn into a nightmare soon enough.
In place of "HERE I NEED A COMBOBOX" you have to write :
<select name='User_Department' id='User_Department'>
$User_Department_html
</select>
However, you retrieve parameters within your sub display_form but you've never passed any.