10 Replies - 515 Views - Last Post: 01 August 2014 - 01:58 PM Rate Topic: -----

#1 DoxramosPS  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 174
  • Joined: 07-October 12

Ajax/PHP Functions

Posted 31 July 2014 - 09:40 PM

I'm trying to find a way to utilize AJAX, but everything I look up gives an ajax URL and I can't seem to find a way to utilize a function. IE
function addUser() {
var valid = true;
allFields.removeClass( "ui-state-error" );
valid = valid && checkLength( name, "username", 3, 16 );
valid = valid && checkLength( email, "email", 6, 80 );
valid = valid && checkLength( password, "password", 5, 16 );
valid = valid && checkRegexp( name, /^[a-z]([0-9a-z_\s])+$/i, "Username may consist of a-z, 0-9, underscores, spaces and must begin with a letter." );
valid = valid && checkRegexp( email, emailRegex, "eg. ui@jquery.com" );
valid = valid && checkRegexp( password, /^([0-9a-zA-Z])+$/, "Password field only allow : a-z 0-9" );
if ( valid ) {

});


Right here at the end of the code all I want to happen is
my $Dox->CreateUser() Function
public function CreateUser() {
		Dox::Connect();
		$stmt = $this->mysqli->prepare("INSERT INTO account (username,email,password,permissions) VALUES ( ?,?,?,?)");
		if($stmt === false) {
			trigger_error($this->mysqli->error, E_USER_ERROR);
			$uname = $_POST['name'] ?:'';
			$email = $_POST['email'] ?:'';
			$password = MD5($_POST['password']) ?:'';
			$permissions = 1 ?:'';
		$stmt->bind_param('sssi', $uname, $email, $password, $permissions);
		$stmt->execute();
		}
	}


I tried this (Not sure why I tried it. I was about 99.9% sure it wouldn't work.
function addUser() {
var valid = true;
allFields.removeClass( "ui-state-error" );
valid = valid && checkLength( name, "username", 3, 16 );
valid = valid && checkLength( email, "email", 6, 80 );
valid = valid && checkLength( password, "password", 5, 16 );
valid = valid && checkRegexp( name, /^[a-z]([0-9a-z_\s])+$/i, "Username may consist of a-z, 0-9, underscores, spaces and must begin with a letter." );
valid = valid && checkRegexp( email, emailRegex, "eg. ui@jquery.com" );
valid = valid && checkRegexp( password, /^([0-9a-zA-Z])+$/, "Password field only allow : a-z 0-9" );
if ( valid ) {
<?php echo $Dox->CreateUser(); ?>
});


Other than that I'm not sure what to do because AJAX has
$.ajax({
type: "POST",
url: url,
data: data,
success: success,
dataType: dataType
});


That's the only real example I can find anywhere, but I have ZERO idea where to start with
$.ajax({
type: "POST",
url: url,//The URL would be ../functions.d/hook.php, but it's in the Dox Class within the CreateUser function so that's a no go.
data: data,//I have no idea what it means. data comes from the input user, email and password inputs.
success: success, //These next two I'm 0 % sure on what they're talking about and I don't know where to go from here on this at all. I'm kind of at a standstill at the moment on this
dataType: dataType
});


Any pointer in the right direction would be greatly appreciated. Haven't really messed with jqery/javascript, but it's a pain in my neck (So was all forms of php when I first started though so we'll see. =D )

Is This A Good Question/Topic? 0
  • +

Replies To: Ajax/PHP Functions

#2 astonecipher  Icon User is offline

  • D.I.C.
  • member icon

Reputation: 795
  • View blog
  • Posts: 3,483
  • Joined: 03-December 12

Re: Ajax/PHP Functions

Posted 01 August 2014 - 12:55 AM

What is used in the data: field is what you are passing to the page, in addition to the url.

Url(/process.php?/)
Data(do=insert)

Then on process.php you would do something like,

if ( isset(get[do]) )
{
   switch (get[do])
   {
      case 'insert':
         // call your function
        break;

   }
}




( I'm on my phone and it already deleted the code twice so, there are errors this time around)

And your class method is to tightly coupled. It should be passed arguments, not dealing with get or post data directly.

This post has been edited by Dormilich: 01 August 2014 - 06:00 AM
Reason for edit:: fixed code

Was This Post Helpful? 0
  • +
  • -

#3 DoxramosPS  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 174
  • Joined: 07-October 12

Re: Ajax/PHP Functions

Posted 01 August 2014 - 06:50 AM

I think I have a slight understanding further on what I need to do here, but I'm still pretty iffy on it. My thought is that I would want to have a new page in my functions folder
EG
functions.d
------------>hook.php
------------>calls.php


Then within calls.php I would use
include_once("hook.php");
if ( isset($_GET[do]) )
{
   switch ($_GET[do])
   {
      case 'insert':
         $Dox->CreateUser();
        break;

   }
}



I still have no idea where you're getting get[do] though as the forms set to a POST method.
I'm also not sure where case insert is coming from. I'm thinking possibly a form named insert so you could have case insert to add and case delete to remove. Not 100% sure on how it all works together though.
Was This Post Helpful? 0
  • +
  • -

#4 astonecipher  Icon User is offline

  • D.I.C.
  • member icon

Reputation: 795
  • View blog
  • Posts: 3,483
  • Joined: 03-December 12

Re: Ajax/PHP Functions

Posted 01 August 2014 - 09:15 AM

You can use get in your Ajax call. Then, based on what value is in the get array, you use the switch statement to execute a specific set of code. The get is also what is in the .Ajax data area. The next thing you would need to do is, pass the post array to your method. The data doesn't move their on its own and as you have it currently written, your class method is to tightly bound to a form. That makes your method non-modular.
Was This Post Helpful? 0
  • +
  • -

#5 DoxramosPS  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 174
  • Joined: 07-October 12

Re: Ajax/PHP Functions

Posted 01 August 2014 - 09:26 AM

I've gotten variables to pass, but I have a problem with using
if(isset($_POST['Insert'])) {
           //Perform Function
}


Instead of executing the function I actually get an alert box with the current pages html inside of it.
Was This Post Helpful? 0
  • +
  • -

#6 DoxramosPS  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 174
  • Joined: 07-October 12

Re: Ajax/PHP Functions

Posted 01 August 2014 - 09:35 AM

What I've found on Ajax so far is
var postData = $("#Insert").serializeArray();
    var formURL = $("#Insert").attr("action");
    $.ajax(
    {
        url : formURL,
        type: "POST",
        data : postData,
        success:function(data, textStatus, jqXHR)
        {
            alert(data);
        },
        error: function(jqXHR, textStatus, errorThrown)
        {
        }
    });


and then for the actual PHP I have
include_once('../functions.d/hook.php');
			if(isset($_POST['Insert'])) {
			$uname = $_POST['name'] ?:'';
			$email = $_POST['email'] ?:'';
			$password = MD5($_POST['password']) ?:'';
			$permissions = 1 ?:'';
			echo $uname;
			}


If I remove the if(isset($_POST['INSERT'])) then it echos the username, but with the isset in place it then displays the raw html in the alert box.
Was This Post Helpful? 0
  • +
  • -

#7 astonecipher  Icon User is offline

  • D.I.C.
  • member icon

Reputation: 795
  • View blog
  • Posts: 3,483
  • Joined: 03-December 12

Re: Ajax/PHP Functions

Posted 01 August 2014 - 09:59 AM

What is the goal of the ternary operators?

$uname = $_POST['name'] ?:'';


The alert(data) is going to show what you sent, if there is no call back code in the php function.

Firebug is a huge asset when coding Ajax by the way.
Was This Post Helpful? 0
  • +
  • -

#8 DoxramosPS  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 174
  • Joined: 07-October 12

Re: Ajax/PHP Functions

Posted 01 August 2014 - 10:11 AM

It's just ripped right from my function where I'm preparing the query so that I can view what it's recieving, so far I have it now executing the case statement; just not the actual post data
var postData = $("#Insert").serializeArray();
    var formURL = $("#Insert").attr("action");
    $.ajax(
    {
        url : "test.php",
        type: "POST",
        data : "do=Insert",
        success:function(data, textStatus, jqXHR)
        {
            alert(data);
        },
        error: function(jqXHR, textStatus, errorThrown)
        {
        }
    });


Doing it that way makes the only post the do insert so now I'm going to try to get the other posts to pass as well. I tried
data2 : postData,
success:function(data, data2, textStatus, jqXHR)


That was unfortunately a no go, but it's getting closer, And thanks for the tip. I'll look into firebug.
Was This Post Helpful? 0
  • +
  • -

#9 DoxramosPS  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 174
  • Joined: 07-October 12

Re: Ajax/PHP Functions

Posted 01 August 2014 - 01:45 PM

Just updating that I've got it working. Ajax is my new pain to learn haha.
My new JS Code is
	var postData = $('form[name="Insert"]').serializeArray();
    var formURL = $("#Insert").attr("action");
    $.ajax(
    {
        url : formURL,
        type: "POST",
        data : postData,
        success:function(data, textStatus, jqXHR)
        {
            alert(data);
        },
        error: function(jqXHR, textStatus, errorThrown)
        {
			alert("Something Went Wrong. Please Try again later.");
        }
    });


Followed by the PHP
include_once('../functions.d/hook.php');
			if(isset($_POST['do'])) 
			{
				switch ($_POST['do'])
					{
					case 'Insert':
					$Dox->CreateUser();
					break;
					}
				}


And within that form I added
<input name="do" value="Insert" type="hidden">


Thanks for the advice in it all astonecipher.
I'm not sure what you mean by my class method being tightly coupled however.
Still just a forum taught guy trying to learn all he can. :)
Was This Post Helpful? 0
  • +
  • -

#10 astonecipher  Icon User is offline

  • D.I.C.
  • member icon

Reputation: 795
  • View blog
  • Posts: 3,483
  • Joined: 03-December 12

Re: Ajax/PHP Functions

Posted 01 August 2014 - 01:49 PM

Glad you got it working.

Tightly coupled refers to the modularity of the code. In OOP style code, each piece is independent of the next and the same blocks can be used to build a variety of programs. A tightly coupled code blocks has a direct relationship with another piece of code and thus limits its reusability in another project, without modification.

Hopefully, that clears it up for you.
Was This Post Helpful? 0
  • +
  • -

#11 DoxramosPS  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 174
  • Joined: 07-October 12

Re: Ajax/PHP Functions

Posted 01 August 2014 - 01:58 PM

A bit it does. Kind of how my Connect Function can be used within any source of code. Just haven't gotten everything down. I figured why not work on the project as I learn and maybe I can end up making something useful in the long haul.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1