Use server value within ajax call

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
sangnandar
User
Posts: 980

Use server value within ajax call

Post by sangnandar »

Edit Page (meaning: $this->id->CurrentValue already exist)
Call ajax and update db.

This code works.
Page_Load():
if (@$POST["myajax"] == 4 && @$POST["value1"] != "" && @$POST["value2"] != "") {
ew_Execute("update myTable set expiry='".$
POST["value1"]."' where id=".$_POST["value2"]);
$this->Page_Terminate();
}
Startup Script:
$.post(ew_CurrentPage(),{ "myajax": 4, "token": EW_TOKEN, "value1": "2017-10-22 19:22:00", "value2": 4379 });

This code DON'T.
Page_Load():
if (@$POST["myajax"] == 4 && @$POST["value1"] != "") {
ew_Execute("update myTable set expiry='".$_POST["value1"]."' where id=".$this->id->CurrentValue);
$this->Page_Terminate();
}
Startup Script:
$.post(ew_CurrentPage(),{ "myajax": 4, "token": EW_TOKEN, "value1": "2017-10-22 19:22:00" });

Debug the sql, it's said:
update myTable set expiry='2017-10-22 19:22:00' where id=. // meaning: $this->id->CurrentValue is empty

Why did $this->id->CurrentValue empty in this Page_Load() server event?


arbei
User
Posts: 9284

Post by arbei »

Refer to the logic of the edit page, in Page_Load Server Event, the record is not loaded yet.


mobhar
User
Posts: 11660

Post by mobhar »

Use "Page_Render" instead of "Page_Load" server event for such case.


sangnandar
User
Posts: 980

Post by sangnandar »

Moved the code to Page_Render().
Page_Render()
if (@$POST["myajax"] == 4 && @$POST["value1"] != "") {
ew_Execute("update myTable set expiry='".$_POST["value1"]."' where id=".$this->id->CurrentValue);
$this->Page_Terminate();
}

It's not working. Now the ajax that was fail (manully set id=4379, the ajax call still fail).

What else can I try?


mobhar
User
Posts: 11660

Post by mobhar »

Remove this line:

$this->Page_Terminate();


sangnandar
User
Posts: 980

Post by sangnandar »

mobhar wrote:
Remove this line:

$this->Page_Terminate();

Didn't work either. Ajax call still fail.
Looks like the server side code has to be put exactly in Page_Load() and nowhere else.


mobhar
User
Posts: 11660

Post by mobhar »

The main reason why you should move your code to "Page_Render", because in "Page_Load", $this->id->CurrentValue is not loaded, yet. This value is loaded in "Page_Render" server event.

If you want to keep your code in "Page_Load", then make sure the value of "id" exists. You may alternatively get the value of "id" using "Startup Script", and then post the value from there using Ajax, and catch again in "Page_Load" server event as POST variable, just like the working part you mentioned in first post.


kirondedshem
User
Posts: 642

Post by kirondedshem »

You intial php code to process ajax call is ok and I thinks its better if its in page_load.
BUT you need to pass the value of $this->id->CurrentValue in the ajax call as well, just find it in page_render and then pass it to a variable in javascript then us it in ajax call, then the php code will get all three values including the id of the record.


sangnandar
User
Posts: 980

Post by sangnandar »

I've managed to do these successfully:

  1. Page_Render(); // pass $this->id->CurrentValue to client side
  2. Startup Script; // include the value in ajax call
  3. Page_Load(); // execute ajax

But I hope there's another solution to do this more straightforward rather than passing the server value forth and back.

Thanks.


Post Reply