Calling PHP script through Ajax and executing SQL

Before I start, I am new to this forum and to Scriptcase. I come from PHPMaker and am VERY CLOSE to switching to SC, but there is one door I just cannot seem to get through, and that has to do with custom coding.

I need to do a custom admin page with some HTML, JS, Ajax (and JSON) behind a few buttons calling a PHP script, which in turn does some SQL on the database. Took me a couple of hours to figure it out in PHPMaker, but here I just cannot strike gold. Here?s the setup:

page.php = the interface page with the buttons (could also be called page.htm, since it contains no php), which calls service.php
service.php = the page that does the SQL and returns an ajax message

I tried 2 alternatives:

  1. adding a custom menu item in SC and simply linking it to page.php
  2. adding a blank page., and in the OnExecute I wrote:

[INDENT]$content = file_get_contents(‘page.php’);
echo($content);
[/INDENT]

Now, both versions actually display the page.php in the correct SC frames. The first solution is very fast, the 2nd one takes a second before all the ?blank? page overhead code is executed. I have absolutely no idea why a blank page needs 1175 lines of code (!!) without displaying anything, but ok, that?s not the topic right now.

When I click on the button in the two alternatives, the ajax call always returns with ?error?. Even if I simplify service.php to 3 lines, I cannot get a ?success? callback.

Here?s a simplified version of the 2 files:

  1. PAGE.PHP or HTM

[INDENT]<html><body>
<script type=“text/javascript” src="/scriptcase/prod/third/jquery/js/jquery.js"></script>
<script type=“text/javascript” src="/scriptcase/prod/third/jquery/js/jquery-ui.js"></script>
<script type=“text/javascript” src="/scriptcase/prod/third/jquery_plugin/cookie/cookie.jquery.js"></script>
<script type=“text/javascript” src="/scriptcase/prod/third/jquery_plugin/treeview/jquery.treeview.js"></script>
<script type=“text/javascript” src="/scriptcase/prod/third/jquery_plugin/layout/jquery.layout.js"></script>

<script type=“text/javascript”>
function callPHP(what) {
$.ajax({
type: ‘POST’, async: false, url: ‘service.php’,
data: ‘cmd=’ + what,
dataType: ‘text’,
success: function (txt) { alert(txt); }
error: function (data) { alert(what + “: ajax failed”); }
});
return false;
}
</script>

<table style=“xxwidth: 100%;” cellpadding=“3px”><tr>
<td style=“xxwidth: 445px”>
<input name=“Button2” onclick=“callPHP(‘CreateTables’)” style=“width: 144px” type=“button” value=“Do SQL”></td>
<td>Do some SQL stuff</td>
</tr></table>
</body></html>
[/INDENT]

  1. SERVICE.PHP

[INDENT]<?php
if (isset($_POST[“cmd”])) { $cmd = $_POST[“cmd”]; } else { $cmd = “”; }
switch($cmd) {

case “DeleteProducts”:
break;

case “CreateTables”:
sc_exec_sq(“INSERT INTO oc_order_status (order_status_id, language_id, name) VALUES (17, 1, ‘Test’);”);
echo $cmd;
break;

default:
echo $cmd . “: command unknown!”
}
?>
[/INDENT]

Why do I always return to ajax ?error?, and why does service.php not run at all? Can I just call sc_exec_sql(), or am I not in the SC mysql scope? What must I include in the header of the 2 files in order to get this running? How do I integrate this kind of code into the SC environment? I searched hours in this forum and found nothing. Is there a coding forum with example code somewhere?

Maybe I?m just thinking too complex and am missing an obvious solution, so if there is one I?d like to know of it, but I?d also like to know how my setup with the two files above could work. I will have more tasks like this in the future, and at the moment I find PHPmaker a little more coding-friendly.

I hope you can change my mind.

Just to clarify things, here’s a screenshot of the two alternatives in the menu:

The html page on the right is PAGE.PHP, which calls SERVICE.PHP, which executes the SQL and returns an Ajax result.

scriptcode.jpg

First you should look at the SC videos, for example Basic Concepts

You can use a control app (with your buttons) and/or a menu app and put your code from service.php in a php method/function (your control app / Programming / PHP Methods.

You can also create a blank app, put your code from service.php to the blank app and call / link it from your control app.

For parameters you can use global vars (your app / Application / Global Variable) or $_SESSION vars ala $_SESSION[‘app’][‘cmd’] = “DeleteProducts”;

Thank you Reinhard. It helped me to understand SC a little better, but it’s not what I want.
Let me put it in simple words:

  1. I want to have my OWN javascript/html form with buttons and other controls.
  2. Button onClick should run PHP code which uses sc_exec (and likewise) functions to execute SQL. How do I do this??
  3. After this, I want to alert(“”) the user if everything went ok. (I know this is covered by SC message functions, but I want to return JSON to the Javascript form)

Or alternatively: How can I tunnel from Javascript to PHP in ScriptCase ?? In PHPMaker I used Ajax & JSON and it worked perfectly. Here, after reading the whole day and watching tutorials, I am still nowhere.

Am I missing something so obvious, that it should be embarrassing?

Ok, that’s no problem.

You make a blank app with SC and put your code with sc_exec … in the event onExecute (without <?php … ?>). You can test the code in SC at Home / <app name> / run-Button.

From your own javascript/html form link to the blank app (i named my blank apps with code_<appname>, code_Autostart etc.). The link refers to <project>/<app_name>/code_<appname>.php.

If that does not work, export a project and hang it on here and i will look at this.

Ok, first part understood. Have tried that. 2nd part is what I’m tripping over. Does that mean my code from above needs to change to: see bold below. Or what do you mean by “link”? If I do a Scripcase link, how can I communicate with my PHP in order to call a function and get a result returned? I thought that’s what good old Ajax is for.

function callPHP(what) {
$.ajax({
type: ‘POST’, async: false, url: ‘project/appname/code_service.php’,
data: ‘cmd=’ + what,
dataType: ‘text’,
success: function (txt) { alert(txt); }
error: function (data) { alert(what + “: ajax failed”); }
});
return false;
}

Yes, when your (SCs) file is at this place.

$.ajax({
type: “POST”,
url: “…/blk_validar_datos/blk_validar_datos.php”,
data: datos,
//dataType: “json”,
success: function(data) {
console.log(data);
}
});
}