Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello ,
I create a WP plugin , it is a form that user can submit some data.
lets first look at the code
PHP
<?php
/*
Plugin Name: My Plugin
Description: test plugin
Version: 4.0
Author: Hercal
License: GPL
*/
include('db.php'); //database connection file located in same folder .
?>

I began my form with :
PHP
<?php function form_creation(){?>
<form action=" <?php get_permalink(); ?> " method="post" id="myform">
<table>

<tr>
<td><input type="text" id="txtname" name="txtname" placeholder="Your Name"/> </td>
</tr>

<tr>
<td>
<select id='select_Country' name="select_Country" >
		<option selected="selected" disabled="disabled"> -- Select Country -- </option>
	    <?php
		include(db.php);
        $query="select Code,Country from _Country order by Country";
        $result=mysqli_query($con,$query);
        while($row=mysqli_fetch_array($result))
        {
            echo '<option value='.$row["Code"].'>'.$row["Country"].'</option>';
        }
        ?>
</select> 

</td>
</tr>
<tr>
<td>  <input type="submit" id="btnsubmit" value="Submit" name='submit'/> </td>
</tr>
</table>
</form>
<?php }?>

and below is the php code in same page to insert values into DB
PHP
<?php
include(db.php);
if($_POST['submit'])
{
	$name=strip_tags($_POST['txtname']);
	$country=$_POST['select_Country'];
$insertquery="insert into _customers(Name,Country)values('$name',(select Country from _country where Code = '$country')";
	$query=mysqli_query($con,$insertquery);
	if($query)
	{
		echo 'Data inserted successfully';
	}
	else
	{
		echo 'there is a problem<br/>';
		print_r( (mysqli_errno($con)));
	}
}
?>


and finally below line also in same page to create a short code for this form
PHP
<?php add_shortcode('test', 'form_creation');?>


Now , when i use (test) shortcode and put it on my wordpress page , form loaded ,displayed but there is one problem .

1-Country field didn't load from database , so the drop down load with only one choice
,Select Country

keep in mind :
1-when I Enter a name and press submit , name inserted successfully to DB which means that db.php has no problem , so why the select country query didn't return values.

2-when i run the same code on root(without WP) , it works fine and country loaded successfully but when i call this plugin from WordPress using ShortCode , problem happens

I need your support , thanks .
Posted
Comments
Robinvb 11-Dec-14 19:34pm    
I had this same kind of problem a while ago, only to figure out Wordpress doesn't send an array back from the database, it handles it like objects, so I needed to use;

$row->column;

If this isn't what you mean, I'm very sorry!
I'll try and look into my own code tomorow if this doesn't help you at all :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900