2tone
Posts: 20
|
Posted: 01/23/2015, 12:21 AM |
|
I am trying to make this painless as possible for myself I am using this code in a separate custom file
require('db.php'); I want to change this to require('common.php');
if(isset($_POST['username']))
{
$username= $_POST['username'];
$sql = mysql_query("select uid from users where usernamer='$username");
if(mysql_num_rows($sql))
{
echo 'Taken.';
}
else
{
echo 'OK';
}
}
now the require db connection is
$conn = mysql_connect("localhost","root","") or die("Database not connected");
$db=mysql_select_db("mydatabase", $conn) or die("Database not connected");
I would like to use the common file like require common but I am having trouble getting it to connect to the common.php
|
|
|
eratech
Posts: 513
|
Posted: 01/23/2015, 2:45 AM |
|
2tone - if you are looking to check a database for some unique id (or even looking for some value in the DB), I would suggest trying some of the Codecharge built-in functions to help out, and save a lot of the custom code.
A lot of the common tasks are set as Event Actions.
Quick and dirty for making usernames unique is to set the 'username' field on the login Record to 'Unique' and it will check automatically (but gives a generic error)
To handle it yourself, add some Server Side Event Actions to the Record's Before Insert (this is from memory so you might need to fill in some properties):
- Declare Variable : tmpUserCheck
- Dlookup: "count(uid)" "users" "username=".CCGetFromPost("username","")
and put it in the variable $tmpUserCheck
- CustomCode:
if ($tmpUserCheck > 0) {
$LoginRecord->AddError("user","Sorry, that Username is already taken");
}
(obviously change the $LoginRecord to the name of your Login form, and when you are debugging, have a look at the Code panel to see what it's actually generated. Helps making little adjustments to the properties.
Check out the Manual for 'PHP Functions' like 'CCGetFromPost()' etc which will be a little safer than plain $_POST[] (which doesn't sanitise or check for dodgy input).
If I've mis-understood, I'm sorry, let me know and I'll have another go.
Cheers
Eric
_________________
CCS 3/4/5 ASP Classic, VB.NET, PHP
Melbourne, Victoria, Australia |
|
|
2tone
Posts: 20
|
Posted: 01/23/2015, 3:44 AM |
|
hey eratech what it is I I am using a ajax and jquery code
jquery is the following
$(document).ready(function () {
$("#username").keyup(function () {
var soldier_name = $("#username").val();
var msgbox = $("#status");
if (username.length > 3) {
$("#status").html('');
$.ajax({
type: "POST",
url: "ajax.php",
data: "username=" + username,
success: function (msg) {
$("#status").ajaxComplete(function (event, request) {
if (msg == 'OK') {
$("#username").removeClass("red");
$("#username").addClass("green");
msgbox.html('Available');
}
else {
$("#username").removeClass("green");
$("#username").addClass("red");
msgbox.html(msg);
}
});
}
});
}
else {
$("#username").addClass("red");
$("#status").html('<font color="#cc0000">To Short</font>');
}
return false;
});
});
but the ajax requires the connection to the database for look up which is
require('db.php');
if(isset($_POST['username']))
{
$username= $_POST['username'];
$sql = mysql_query("select uid from users where usernamer='$username");
if(mysql_num_rows($sql))
{
echo 'Taken.';
}
else
{
echo 'OK';
}
}
I want to change where it requires the db.php and use the common.php so I don't have to have 2 connections to the db
originally from here
http://www.2my4edge.com/2013/07/username-live-availability-check-using.html
|
|
|
eratech
Posts: 513
|
Posted: 01/24/2015, 6:22 PM |
|
Ok - this is one of the common problems when starting to use CodeCharge- trying to insert regular code snippets into CCS and it breaks (and yes, I did it myself when starting out)
CodeCharge uses wizards and XML, and the visual designer to generate code and essentially has its own framework to handle all the DB connections and updates, validation, events, etc (like CakePHP, Laravel, etc etc etc)
The sample you are using is what you would do with plain PHP code and it is more work to try and shoe-horn it into CodeCharge and get it working, than it is to try and use the CCS features to do the same job.
Essentially you are taking a username from the form, checking the database via AJAX and returning a Taken/Ok message. You can get CCS to do all this through a "Feature" (from memory it is a 'Remote DB Service' - see the Record property called 'Features') and then you can do some Custom Code javascript to make the message appear on form (similar to the above code).
And if CCS creates the Service or other files, it will handle the DB connections for you. No need to do extra connections.
I can't get the exact code right now, but you should look at the "Feature" and "Service" documentation and Remote DB events, and if you haven't already I strongly suggest downloading from yessoftware the Example packs (I think they are in the Support > CCS Customer Area (you need your Serial Number to access))
Many of the examples are also described in the Help file or online at 'Documents and Tutorials', but the AJAX examples are pretty much only in the Customer Area.
Sorry it's more reading.
Eric
_________________
CCS 3/4/5 ASP Classic, VB.NET, PHP
Melbourne, Victoria, Australia |
|
|
MichaelMcDonald
Posts: 640
|
Posted: 01/29/2015, 3:23 AM |
|
For username just set to Unique in the field properties and let ccs do the lookup for you.
_________________
Central Coast, NSW, Australia.
|
|
|
eratech
Posts: 513
|
Posted: 01/29/2015, 5:32 PM |
|
MichaelMcDonald - yeah, that was my 'quick and dirty' method - you don't get flexibility with the returned message but it does work.
For my own fun I'm seeing if it's possible with the built-in AJAX and RemoteDB stuff to do it with CCS and not a lot of custom code.
E
_________________
CCS 3/4/5 ASP Classic, VB.NET, PHP
Melbourne, Victoria, Australia |
|
|
MichaelMcDonald
Posts: 640
|
Posted: 02/01/2015, 4:11 AM |
|
On validate event
$username = $container->username->GetValue();
$db = new clsDBConnection();
$SQL = "SELECT id FROM users WHERE username = '$username' ";
$db->query($SQL);
if($db->next_record()){
$id = $db->f("id");
}
if($id > 0){
$container->Errors->addError("username already taken");
}
$db->close();
_________________
Central Coast, NSW, Australia.
|
|
|
|