Jump to content

calling a php routine in the same page


jbond

Recommended Posts

Hi everyone,

I am trying to call a routine within the same program when a user fills out a form and hits the submit key. I am sure that this is feasible, but big question is on how to accomplish this

 

The form takes several inputs and should then be redirected to a routine that will eventually display the query results, but this within the same program, so I do not want to link the form to a call to an external php program . I think that I may have to use the extension

onclick=" ", but the big question then is how to call the php routine that does all the checking??

Below is the extract of the form that is being used..

 

 

        <td colspan="4"><div align="center">PLEASE SELECT ONE OF THE ABOVE QUERY POSSIBILITIES </div></td>

        </tr>

      <tr>

        <td><input type="submit" name="Submit" value="Submit" /></td>

        <td> </td>

        <td> </td>

        <td> </td>

      </tr>

    </table>

  </form>

 

 

Thanks very much for your kind input

 

B rgds

Link to comment
https://forums.phpfreaks.com/topic/101548-calling-a-php-routine-in-the-same-page/
Share on other sites

The thing you want is either ajax or an easier way of passing a variable and catching it on the reload to build your sql query off of the variable description.  For instance:

<?php

if($_REQUEST['VIEW'] == "showall" || $_REQUEST['VIEW'] == "")$sqlQuery = "SELECT * FROM `table`";
if($_REQUEST['VIEW'] == "late")$sqlQuery = "SELECT * FROM `table` WHERE `Status` = 'Late'";

//Execute your query here.


?>
<a href='index.php?VIEW=showall'>Show Late</a>
<a href='index.php?VIEW=late'>Show Late</a>

 

I think this is where your going with this but if you want to display the results on the page without reloading you need ajax. If you want to just query out the user data you just entered and loading a page is not an issue just go to another page.

Sounds like you need to look into some AJAX. The basic idea is that javascript makes a request to another php file 'behind the scenes' then updates the page with the output from that page.

 

I suggest you google for some AJAX tutorials to get you started.

This is my first attempt with xajax.. however, it doesn't display the contents of var $container. Where am I going wrong??

 

Below is my test code ..

 

 

 

<?php require_once("xajax_core/xajax.inc.php");

?>

<?php

$xajax = new xajax();

$xajax->registerFunction("getdata");

 

function getdata($arg){

  $container=$arg['container']; 

  echo $container;

}

 

?>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Untitled Document</title>

</head>

 

<body>

<form id="form1" name="form1" method="post" action="">

  <p>container:

    <input type="text" name="container" />

</p>

  <p>

    <input type="submit" name="Submit" value="Submit"  onclick="xajax_getdata(xajax.getFormValues('form1'))"/ />

</p>

</form>

</body>

</html>

 

Im not for sure but I think there is more you have to do int he php function you register with xajax(your getdata function) I haven't been able to straighten out the part on xajax when it comes to grabbing form data. I will continue to play and if you find the way to use form data with xajax please contact me! I will do the same.

following code works for me (proof of concept). By putting the array elements into $_SESSION vars, I can use them in the main routine...

 

 

<?php require_once("xajax_core/xajax.inc.php");

?>

<?php

session_start();

$xajax = new xajax();

$xajax->registerFunction("getdata");

$xajax->processRequest();

 

function getdata($arg){

  $container=$arg['container'];

  $namis=$arg['namis'];

  $_SESSION['cont']= $container;

  $_SESSION['namis']=$namis;

}

 

?>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Untitled Document</title>

</head>

 

<body>

<?php

    /*** print the javasript ***/

    $xajax->printJavascript();

?>

<form id="form1" name="form1" method="post" action="">

  <p>container:

    <input type="text" name="container" />

</p>

  <p>namis number

    <input type="text" name="namis" />

  </p>

  <p>

    <input type="submit" name="Submit" value="Submit"  onclick="xajax_getdata(xajax.getFormValues('form1'))"/ />

</p>

</form>

 

<?php

$show_cont=$_SESSION['cont'];

$show_namis=$_SESSION['namis'];

echo $show_cont . " " . $show_namis;

?>

 

 

</body>

</html>

You invoke some class functions within your xajax defined function. I am not for sure about forms but:

<?php
function doAdd($a, $b)
{
   $response = new xajaxResponse();
   $response->assign('answer', 'innerHTML', $a + $b);
   $response->assign('reset', 'style.display', 'block');
   return $response;
}
?>

Is an example they use for working with forms on their site at http://xajaxproject.org/

 

Also, here is a function that I used and it had some interesting class functions that would work with your return data before returning it to the main script:

There are seven steps in which I have commented to explain. When returning something a an html object you have to tell your php function defined for xajax where it needs to go and invoke new xajaxResponse()', and then return that variable to like this: $objResponse->assign("elemID1","innerHTML", $foundThis); then return it like this: return $objResponse;

<?php
//1. Include the xajax class library:
require('xajax/xajax_core/xajax.inc.php');

//2. Instantiate the xajax object:
$xajax = new xajax();

//3. Register the names of the PHP functions you want to be able to call through xajax:
$xajax->registerFunction("checkThis1");
$xajax->registerFunction("checkThis2");
$xajax->registerFunction("randUpdate");

//4. Write the PHP functions you have registered and use the xajaxResponse object to return XML commands from them:
function checkThis1(){
   //Columns:
   //id
   //nameWhole
   //someNum
   $sqlQuery = "SELECT `someNum` FROM `xajax` WHERE `nameWhole` = 'Name'";
   $sqlResult = mysql_query( $sqlQuery );
   $rowCheck = mysql_fetch_assoc($sqlResult);
   $foundThis = $rowCheck["someNum"];

   // Instantiate the xajaxResponse object
   $objResponse = new xajaxResponse();

   // add a command to the response to assign the innerHTML attribute of
   // the element with id="SomeElementId" to whatever the new content is
   $objResponse->assign("elemID1","innerHTML", $foundThis);

   //Return the object
   return $objResponse;
}
//5. Before your script sends any output, have xajax handle any requests:
$xajax->processRequest();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   <title>xajax Testing</title>
   <?php $xajax->printJavascript("xajax/"); ?>
   <style type="text/css">
       a { text-decoration : none;
           color : #000000;
       }
       a:visited { text-decoration : none;
           color : #000000;
       }
       a:hover {
           color : #F5AE25;
           background : ;
       }
       .hdr { text-decoration : none;
           color : #ff0000;
       }
       .hdr:visited { text-decoration : none;
           color : #ff0000;
       }
       .hdr:hover {
           color : #F5AE25;
           background : #bb0000;
       }
       .hihiterow:hover {
           color : #F5AE25;
           background : #ffffff;
       }

</style>
<script type="text/javascript">
function initializeAll(){ //Creates a control for starting async stuff
   checkBoth();
   updateRand();
   //setTimeout('startNow()',1000);
}
function updateRand(){
   xajax_randUpdate();
   setTimeout('updateRand()',1000);
}
function checkBoth(){
   xajax_checkThis1();
   xajax_checkThis2();
   setTimeout('checkBoth()',1000);
}
</script>

</head>
<body style="font-family: arial; font-size: 14px;" onLoad="initializeAll()">

<!--7. Call the function from a JavaScript event or function in your application:-->

<big>My Number<br>
<div id="elemID1"></div></big>
<big>Somebody's number<br>
<div id="elemID2"></div></big>
<br><br>
<? echo date("h:m:s") . " <-The time stamp is not updating and is created once upon page loading. Monitor this and change the database value.  The number should change but the non-changing timestamp indicated the page is not reloading. AJAX in work.<br><br>";?>
</body>
</html>

 

I used this to test sending chat messages to different channels and retrieving each channel. I only included one of the xajax functions for one channel.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.