The correct way to end PHP script called by AJAX request

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 1163 Views - Last Post: 27 March 2018 - 01:20 PM Rate Topic: -----

#1 Foobarer   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 261
  • Joined: 28-March 17

The correct way to end PHP script called by AJAX request

Posted 26 March 2018 - 02:03 PM

I have for example update.php that is called by an AJAX request.
Is it OK to use exit() after returning data back to the AJAX request, so that it won't echo the rest
Like so:

if (statement1){
    ..echo json_encode ..
    exit();

//do more stuff here

if(statement2) {
    ..echo json_encode ..
    exit();
} //more statements




This post has been edited by Foobarer: 26 March 2018 - 02:04 PM

Is This A Good Question/Topic? 0
  • +

Replies To: The correct way to end PHP script called by AJAX request

#2 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3796
  • View blog
  • Posts: 13,742
  • Joined: 08-August 08

Re: The correct way to end PHP script called by AJAX request

Posted 26 March 2018 - 02:19 PM

That will work, but you'll end up with spaghetti code. It's better to use an OOP approach.

$data = $someObj->someMethod($someInput);// Now the decision on what to return is determined by the object. 
// If you use a factory class, even the object might be determined by the inputs.
echo json_encode($data); 


Was This Post Helpful? 1
  • +
  • -

#3 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3796
  • View blog
  • Posts: 13,742
  • Joined: 08-August 08

Re: The correct way to end PHP script called by AJAX request

Posted 26 March 2018 - 02:42 PM

Since Ajax requests often involve accessing a database, you might want to look into CRUD. The code below is just a skeleton, so it won't actually do anything other than (hopefully) give you some ideas.
<?php

class AjaxResponse
{
    protected $conn;

    public function __construct(&$conn)
    {
        $this->conn = $conn;
    }

    public function create($data)
    {
        // Create a new row in a table.
        return $row
    }

    public function read($data)
    {
        // Read data from table.
        return $row;
    }

    public function update($data)
    {
        // Update a row.
        return $row;
    }

    public function delete($data)
    {
        // Delete row from table.
        return [];
    }

}

require "connection.php";

$response = new AjaxResponse($conn);
$method = $_POST['method'];

echo json_encode($response->$method($_POST));


Was This Post Helpful? 1
  • +
  • -

#4 Foobarer   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 261
  • Joined: 28-March 17

Re: The correct way to end PHP script called by AJAX request

Posted 26 March 2018 - 02:52 PM

My entire code is already spaghetti since I am a bad coder. I really want to do OOP but I also really want to see progress on the website.
I know that it will take me time to become more efficient, I already have 1 class that is built with CRUD, I saw this tutorial: (this is the first video of about 4 where he makes this 'DB' class)

OOP CRUD Class from a Youtube Tutorial
He made a really nice CRUD class, with basic SQL statements (Not complicated like SELECT WHERE AND AND AND ... - but I can somehow learn by trying to update this myself). But it uses the binding with questions mark (which I prefer less)

Also in the class he doesn't distinct between the types of param bindings. I saw somewhere a nice class with customized "bind" function that sets the bindValue to the correct type, like that:

private function bind($param, $value, $type = null){
        if (is_null($type)) {
            switch (true) {
                case is_int($value):
                    $type = PDO::PARAM_INT;
                    break;
                case is_bool($value):
                    $type = PDO::PARAM_BOOL;
                    break;
                case is_null($value):
                    $type = PDO::PARAM_NULL;
                    break;
                default:
                    $type = PDO::PARAM_STR;
            }
        }
        $this->stmt->bindValue($param, $value, $type);


So I am using this class he made for some statements, but when I really want to just progress I make Spaghetti bolognese.

But I am slowly also using OOP (Hopefully it will replace the mess in the future)

This post has been edited by Foobarer: 26 March 2018 - 02:56 PM

Was This Post Helpful? 0
  • +
  • -

#5 Foobarer   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 261
  • Joined: 28-March 17

Re: The correct way to end PHP script called by AJAX request

Posted 26 March 2018 - 04:15 PM

View PostCTphpnwb, on 26 March 2018 - 02:42 PM, said:

Since Ajax requests often involve accessing a database, you might want to look into CRUD. The code below is just a skeleton, so it won't actually do anything other than (hopefully) give you some ideas.
<?php

class AjaxResponse
{
  ..class code here..
}



I have a question regarding these types of classes (using CRUD) and code duplication:
Let's say I have a few scripts that need the usage of the CRUD part of the class: Registration, Login, Ajax calls, fetching data to display, etc: Do you create a class for each, copying the CRUD part, and add to each class its own methods, or you'd simply make a general class with many methods?
Or for some you'd do that and others you'd separate? I was thinking that Login and Registration can be in the same class called User for example, and the Ajax is a separate, although it copies exactly the same CRUD part (Just wonder because it's duplicating code isn't it?)

**BtW: Why your delete method returns [] ?

This post has been edited by Foobarer: 26 March 2018 - 04:24 PM

Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3796
  • View blog
  • Posts: 13,742
  • Joined: 08-August 08

Re: The correct way to end PHP script called by AJAX request

Posted 26 March 2018 - 06:25 PM

One of the main purposes of OOP is reuse. You might look into inheritance (in php a class can 'extend' a parent).

Since the other methods return arrays, for consistency I chose to have the delete method return an empty array. You might have it return the original row if it couldn't delete it for some reason.
Was This Post Helpful? 1
  • +
  • -

#7 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3796
  • View blog
  • Posts: 13,742
  • Joined: 08-August 08

Re: The correct way to end PHP script called by AJAX request

Posted 26 March 2018 - 06:32 PM

I forgot to mention composition. A class can take advantage of another class by creating an instance of it:

class foo {
	public function bar() {
		echo "foo bar!";
	}
}

class bar {
	public function foo() {
		echo "bar foo...";
		$obj = new foo();
		$obj->bar();
	}
}

$anObj = new bar();
$anObj->foo();


Was This Post Helpful? 1
  • +
  • -

#8 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2560
  • View blog
  • Posts: 10,277
  • Joined: 03-December 12

Re: The correct way to end PHP script called by AJAX request

Posted 26 March 2018 - 09:12 PM

View PostFoobarer, on 26 March 2018 - 04:52 PM, said:

private function bind($param, $value, $type = null){
        if (is_null($type)) {
            switch (true) {
                case is_int($value):
                    $type = PDO::PARAM_INT;
                    break;
                case is_bool($value):
                    $type = PDO::PARAM_BOOL;
                    break;
                case is_null($value):
                    $type = PDO::PARAM_NULL;
                    break;
                default:
                    $type = PDO::PARAM_STR;
            }
        }
        $this->stmt->bindValue($param, $value, $type);


You don't actually need to do that, in my experience, and you can actually get creative with the sql as well.


for instance,
SELECT cust_id FROM customer_vehicles WHERE `year` = ? AND model = ? AND make = ?


<?php

$_POST["c_year"] = 1999;
$_POST["c_make"] = "Acura";
$_POST["c_model"] = "MDX";


function addFilter($data){
	$arr = [];
	$values = [];
	foreach($data as $k=>$v){
		$rep = str_replace("c_", "",$k) ." = ?";
		$arr["keys"][] = $rep;
		$arr["values"][] = $v;
	}
	return $arr;
}

$sql = "SELECT cust_id FROM customer_vehicles";
$values = [];
if(!empty($_POST)){
    $filter = addFilter($_POST);
    $sql .= " WHERE " . implode("AND " , $filter["keys"]);
    $values = $filter["values"];
}
echo $sql;

//$stmt = $pdo->prepare($sql);
//$stmt->execute($values);

print_r($values);

Was This Post Helpful? 1
  • +
  • -

#9 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3796
  • View blog
  • Posts: 13,742
  • Joined: 08-August 08

Re: The correct way to end PHP script called by AJAX request

Posted 26 March 2018 - 09:46 PM

As for binding types, I like using arrays:
$types = [
    'integer' => PDO::PARAM_INT,
    'boolean' => PDO::PARAM_BOOL,
    'NULL' => PDO::PARAM_NULL,
    'string' => PDO::PARAM_STR
];

$testValues = ["test string", NULL, 33, false];

foreach ($testValues as $test) {
    echo $types[gettype($test)] . "<br>";
}

Was This Post Helpful? 1
  • +
  • -

#10 Foobarer   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 261
  • Joined: 28-March 17

Re: The correct way to end PHP script called by AJAX request

Posted 27 March 2018 - 02:33 AM

View PostCTphpnwb, on 26 March 2018 - 06:25 PM, said:

One of the main purposes of OOP is reuse. You might look into inheritance (in php a class can 'extend' a parent).

Since the other methods return arrays, for consistency I chose to have the delete method return an empty array. You might have it return the original row if it couldn't delete it for some reason.



View PostCTphpnwb, on 26 March 2018 - 06:32 PM, said:

I forgot to mention composition. A class can take advantage of another class by creating an instance of it:


Thanks, I can really imagine my entire code shrinking if I will implement that correctly, I am going to work on my classes

View Postastonecipher, on 26 March 2018 - 09:12 PM, said:

You don't actually need to do that, in my experience, and you can actually get creative with the sql as well )
for instance,
SELECT cust_id FROM customer_vehicles WHERE `year` = ? AND model = ? AND make = ?


<?php

$_POST["c_year"] = 1999;
$_POST["c_make"] = "Acura";
$_POST["c_model"] = "MDX";


function addFilter($data){
	$arr = [];
	$values = [];
	foreach($data as $k=>$v){
		$rep = str_replace("c_", "",$k) ." = ?";
		$arr["keys"][] = $rep;
		$arr["values"][] = $v;
	}
	return $arr;
}

$sql = "SELECT cust_id FROM customer_vehicles";
$values = [];
if(!empty($_POST)){
    $filter = addFilter($_POST);
    $sql .= " WHERE " . implode("AND " , $filter["keys"]);
    $values = $filter["values"];
}
echo $sql;

//$stmt = $pdo->prepare($sql);
//$stmt->execute($values);

print_r($values);



View PostCTphpnwb, on 26 March 2018 - 09:46 PM, said:

As for binding types, I like using arrays:


Thanks, I really got many ideas, all I have to do is probably choose and stick to one.
CTphpnwb, in your case, you have to write the SQL - is that the approach you take regularly? I mean I saw classes, that to call the methods you can do some neat thing like that:

$this->db->delete('table_name', 'table_column', $user->values);
Now I need to combine your approach with making the sql 'hidden' like the statement above and it would be perfect
your approach is awesome, I really like it

This post has been edited by Foobarer: 27 March 2018 - 02:59 AM

Was This Post Helpful? 0
  • +
  • -

#11 Foobarer   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 261
  • Joined: 28-March 17

Re: The correct way to end PHP script called by AJAX request

Posted 27 March 2018 - 06:05 AM

Update: I was thinking about the following db class calls:

For each of the 4 (create, read, update, delete) I will make the basic SQL statement:
    //skeleton
    public function get($table, $fields, $where = array()) {
        return $this->action("SELECT {$fields}", $table, $where);
    }    

    public function delete($table, $where = array()) {
        return $this->action('DELETE', $table, $where);
    }

    public function insert($table, $fields = array()) {
          //more code here, just a skeleton
        $sql = "INSERT INTO user {$fields} VALUES ({$values})";      
    }
        
    public function update($table, $id, $fields) {
        $sql = "UPDATE {$table} SET {$set} WHERE user_id = {$id}";
    }



But, for each method I will add another paramter called $extra for example:

public function insert($table, $fields = array(), $extra)

And the $extra will contain the extra statement such as 'AND.. AND.. AND...'
and you call the method like so:

$userInsert->insert('table_name', array(
'field1' => 'value1',
'field2' => 'value2'
), "WHERE .. AND .. AND");

It does make it look better, instead of having to take care of all the existing statements, so I will take care of the base, and for other more complex statements I will manually write them. Or it will be bad for some cases? (super long statements? but why would you need a long $extra statement? I am thinking of scenarios where I will need the $extra to be messy - but it should mostly be short)

This post has been edited by Foobarer: 27 March 2018 - 06:21 AM

Was This Post Helpful? 0
  • +
  • -

#12 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2560
  • View blog
  • Posts: 10,277
  • Joined: 03-December 12

Re: The correct way to end PHP script called by AJAX request

Posted 27 March 2018 - 06:27 AM

Honestly, while we showed some shortcuts, you are better off for now writing everything out. Create a model, write out the sql that populates the model. What we showed is more of steps taken later when you need to do something a different way. Understand the basics and build on those first.
Was This Post Helpful? 1
  • +
  • -

#13 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3796
  • View blog
  • Posts: 13,742
  • Joined: 08-August 08

Re: The correct way to end PHP script called by AJAX request

Posted 27 March 2018 - 08:59 AM

And NEVER put data {{ $values }} in a query. Always use prepared statements.
Was This Post Helpful? 1
  • +
  • -

#14 Foobarer   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 261
  • Joined: 28-March 17

Re: The correct way to end PHP script called by AJAX request

Posted 27 March 2018 - 11:16 AM

The $value is just a string that is replaced. I am working on my class already, would love to get your expert opinion:
This is the 'get' method for example:

<?php

function get($table, $fields = array(), $extra = array()) {
    $sql = "SELECT"; // sql starts here
    $i = 0; //counter to prevent extra comma later
    while ($i < count($fields)){
        $sql .= " $fields[$i]";
        $i++;
        if ($i < count($fields)){ // append a comma only if it's not the last field, otherwise SQL won't work with extra comma
            $sql .= ', '; 
        }
    }
    
    foreach ($extra as $extra=>$statement){
        $sql .= " $extra $statement ";
    }    
   
    //here we bind of course but for the example I echo
     echo $sql;
}

//usage example: (ofcourse when it's a finished class it's going to be something like $user->get(..)
get('user', array('user_id', 'username'), array(
    "WHERE" => 'user_id = :user_id',
    "AND" => 'user_name = :user_name'
));


//output:
//SELECT user_id, username WHERE user_id = :user_id AND user_name =:user_name



**This way you don't have to worry about imploding extra things and doing checks, because you add it yourself, although it does make it longer, but I think it's longer but very comfort to look at and write, what do you think? Is it still too long? Or OK?

This post has been edited by Foobarer: 27 March 2018 - 11:29 AM

Was This Post Helpful? 0
  • +
  • -

#15 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2560
  • View blog
  • Posts: 10,277
  • Joined: 03-December 12

Re: The correct way to end PHP script called by AJAX request

Posted 27 March 2018 - 11:28 AM

Don't worry about the abstraction layer at this point...

Sigh, look at how ORM's are built, first.

Next, don't need to do this,
$sql .= ', ';

implode() is what you want.


" $extra $statement ";

is a literal drop of values into the sql statement which is very risky.


Again, rolling your own is great to learn with, but generally not something you want to do in a public site. Look into Doctrine
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2