Multiple page PHP calling scripts

Any and all non-support discussions

Moderators: gerski, enjay, williamconley, Op3r, Staydog, gardo, mflorell, MJCoate, mcargile, Kumba, Michael_N

Multiple page PHP calling scripts

Postby karlb » Wed Mar 27, 2013 11:50 pm

Hello all,

I'm not sure if something of this nature has been posted before and I highly doubt I have a perfect example of how to do this, but I thought I would share what I have done to create multiple page scripts in PHP.

First, the script is called from iframe: (Note is starts at page 1, YOURSCRIPT.PHP?page=1)
Code: Select all
<iframe src="../agc/YOURSCRIPT.PHP?page=1&CF_uses_custom_fields=Y&lead_id=--A--lead_id--B--&vendor_id=--A--vendor_lead_code--B--&list_id=--A--list_id--B--&gmt_offset_now=--A--gmt_offset_now--B--&phone_code=--A--phone_code--B--&phone_number=--A--phone_number--B--&title=--A--title--B--&first_name=--A--first_name--B--&middle_initial=--A--middle_initial--B--&last_name=--A--last_name--B--&address1=--A--address1--B--&address2=--A--address2--B--&address3=--A--address3--B--&city=--A--city--B--&state=--A--state--B--&province=--A--province--B--&postal_code=--A--postal_code--B--&country_code=--A--country_code--B--&gender=--A--gender--B--&date_of_birth=--A--date_of_birth--B--&alt_phone=--A--alt_phone--B--&email=--A--email--B--&security_phrase=--A--security_phrase--B--&comments=--A--comments--B--&user=--A--user--B--&pass=--A--pass--B--&campaign=--A--campaign--B--&phone_login=--A--phone_login--B--&fronter=--A--fronter--B--&closer=--A--user--B--&group=--A--group--B--&channel_group=--A--group--B--&SQLdate=--A--SQLdate--B--&epoch=--A--epoch--B--&uniqueid=--A--uniqueid--B--&rank=--A--rank--B--&owner=--A--owner--B--&customer_zap_channel=--A--customer_zap_channel--B--&server_ip=--A--server_ip--B--&SIPexten=--A--SIPexten--B--&session_id=--A--session_id--B--" style="background-color:transparent;" scrolling="auto" frameborder="0" allowtransparency="true" id="popupFrame" name="popupFrame"  width="--A--script_width--B--" height="--A--script_height--B--" STYLE="z-index:17"> </iframe>


The problem with the PHP was that is had all the variables you just threw at it in the GET request, but the second you click on a link to go to the next page, it forgets all of those. I created a PHP session in the beginning of my script which "holds" the lead you are working with. From that, it creates two arrays; one for the vicidial standard fields and one for custom fields (if you have any). From there, you have a "shell" or template to create scripts in a more timely fashion.

Here is my example, note page 1, 2 and 3. Disposition page, etc. I know it's not perfect, but if I had this when I first fired up vici, it would have saved me at least some headache in programming my first complex script (almost 1,000 line multiple page script!) I'm passing it along in hopes it may help another person dive into multiple page scripts.

Code: Select all

<?
//error_reporting(~0);
//ini_set('display_errors', 1);
$url = $_SERVER['PHP_SELF'];
$webip = "172.24.98.83";
$dbip = "172.24.98.82";
$dbuser = "username";
$dbpass = "password";

mysql_connect($dbip, $dbuser, $dbpass)or die("cannot connect");
mysql_select_db("asterisk")or die("cannot select DB");
session_start();

if (isset($_POST["page"]))   {$page=$_POST["page"];}
   elseif (isset($_GET["page"]))   {$page=$_GET["page"];}
if (isset($_POST["list_id"]))   {$list_id=$_POST["list_id"];}
   elseif (isset($_GET["list_id"]))   {$list_id=$_GET["list_id"];}
if (isset($_POST["campaign"]))   {$campaign=$_POST["campaign"];}
   elseif (isset($_GET["campaign"]))   {$campaign=$_GET["campaign"];}
if (isset($_POST["user"]))   {$user=$_POST["user"];}
   elseif (isset($_GET["user"]))   {$user=$_GET["user"];}
if (isset($_POST["pass"]))   {$pass=$_POST["pass"];}
   elseif (isset($_GET["pass"]))   {$pass=$_GET["pass"];}
if (isset($_POST["lead_id"]))   {$lead_id=$_POST["lead_id"];}
   elseif (isset($_GET["lead_id"]))   {$lead_id=$_GET["lead_id"];}
if (isset($_POST["vendor_id"]))   {$vendor_id=$_POST["vendor_id"];}
   elseif (isset($_GET["vendor_id"]))   {$vendor_id=$_GET["vendor_id"];}
   $vendor_lead_code = $vendor_id;

if($lead_id != $_SESSION['lead_id'] and $page == "1") {
$_SESSION['list_id'] = NULL;
$_SESSION['lead_id'] = NULL;
$_SESSION['user'] = NULL;
$_SESSION['pass'] = NULL;
$_SESSION['campaign'] = NULL;

}

if(!isset($_SESSION['list_id'])) {$_SESSION['list_id'] = $list_id;}
if(!isset($_SESSION['lead_id'])) {$_SESSION['lead_id'] = $lead_id;}
if(!isset($_SESSION['user'])) {$_SESSION['user'] = $user;}
if(!isset($_SESSION['pass'])) {$_SESSION['pass'] = $pass;}
if(!isset($_SESSION['campaign'])) {$_SESSION['campaign'] = $campaign;}
$lead_id = $_SESSION['lead_id'];
$list_id = $_SESSION['list_id'];
$user = $_SESSION['user'];
$pass = $_SESSION['pass'];
$campaign = $_SESSION['campaign'];

//Create array vicifield for vicidial standard fields. Accessed with $vicifield['anystandardfieldname']
$sql="SELECT * FROM vicidial_list WHERE list_id='$list_id' and lead_id='$lead_id'";
$result=mysql_query($sql);
$vicifield = mysql_fetch_assoc($result);

//Create array customfield for any campaign with custom fields. Comment out for campaign without custom fields. Accessed with $customfield['anycustomfieldname']
$sql2="SELECT * FROM custom_$list_id WHERE lead_id='$lead_id'";
$result2=mysql_query($sql2);
$customfield = mysql_fetch_assoc($result2);

?>
<?

// PAGE 1
if($page == "1"){
?>

Hi, My name is ____. May I speak to <? echo $vicifield['first_name']; ?>?


<form method="POST" action="<? echo $_SERVER['PHP_SELF'].'?page=2'; ?>">
<table border="0" width="100%">
   <tr>
      <td>   <p><input type="submit" value="Go to next page" name="B1"></p>
</form></td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>
<form method="POST" action="<? echo $_SERVER['PHP_SELF'].'?page=dispcall'; ?>">
   <p><select size="1" name="status">
   <option value="20">NOT INTERESTED</option>
   <option value="22">OTHER</option>
   <option value="26">DO NOT CALL</option>
   <option value="30">DISCONNECTED</option>
   </select><input type="submit" value="End Call as selected" name="B2"></p>
</form>
</td>
      <td>&nbsp;</td>
   </tr>
</table>
<hr>

<?
}

// PAGE 2
if($page == "2"){
//Update first name field from post form on page 1. Refere to Agent API.
$first_name = $_POST['first_name'];
$update = file_get_contents("http://$webip/agc/api.php?source=$campaign&user=$user&pass=$pass&function=update_fields&agent_user=$user&vendor_lead_code=$lead_id&first_name=$first_name");
?>
This is page 2!

<form method="POST" action="<? echo $_SERVER['PHP_SELF'].'?page=3'; ?>">
<input type="submit" value="Go to next page" name="B1">
</form>

<?
}

// PAGE 3

if($page == "3"){
?>
This is page 3!


<?
}


// DISPOSITION ACTION

if($page == "dispcall"){
$status = $_POST['status'];

$update = file_get_contents("http://$webip/agc/api.php?source=$campaign&user=$user&pass=$pass&agent_user=$user&function=external_hangup&value=1");
$update2 = file_get_contents("http://$webip/agc/api.php?source=$campaign&user=$user&pass=$pass&agent_user=$user&function=external_status&value=$status");

echo "Ending call as $status";
//echo $update;
//echo $update2;
}

// DISPOSITION SELECT

if($page == "dispselect"){
?>
<p></p>

<form method="POST" action="<? echo $_SERVER['PHP_SELF'].'?page=dispcall'; ?>">
   <p><select size="1" name="status">
   <option value="20">NOT INTERESTED</option>
   <option value="22">OTHER</option>
   <option value="26">DO NOT CALL</option>
   <option value="30">DISCONNECTED</option>
   </select><input type="submit" value="Continue" name="B2"></p>
</form>

<?
}

?>

</body>

</html>


Karl Babcock
Last edited by karlb on Tue Jun 04, 2013 5:09 pm, edited 1 time in total.
karlb
 
Posts: 37
Joined: Fri Dec 28, 2012 1:03 pm
Location: Chicago, IL

Re: Multiple page PHP calling scripts

Postby williamconley » Thu Mar 28, 2013 10:18 am

excellent post. consider modifying your code to mysqli (mysql is no longer recommended ...). also consider using the standard vicidial "include" to acquire either the dblink itself (still presently mysql) or you can use it to acquire the DB credentials. this avoids the hard-coding of db credentials in the file itself. removes the need for the "next person" to modify this file for db credentials and you can also move it freely among your own servers without changes. It also reduces the likelihood of exposing the credentials in case of a server failure. PHP fails on boot because you edited a conf file ... and the php files are often sent as text. But includes don't work in that situation so the credentials are safe if they are in an include. :)

and you could consider posting these to the Vicidial Issue tracker ... who knows, it may get included in an extras folder somewhere 8-)
Vicidial Installation and Repair, plus Hosting and Colocation
Newest Product: Vicidial Agent Only Beep - Beta
http://www.PoundTeam.com # 352-269-0000 # +44(203) 769-2294
williamconley
 
Posts: 20258
Joined: Wed Oct 31, 2007 4:17 pm
Location: Davenport, FL (By Disney!)

Re: Multiple page PHP calling scripts

Postby brett05 » Thu Mar 28, 2013 1:54 pm

put your code in mantis please
Jasperreports & Queuemetrics & SugarCRM integration - Customization and Add-ons
Freepbx||Billing||Centos||Opensuse||Debian||Centos||Fedora||Sangoma||Diguim
brett05
 
Posts: 571
Joined: Sun May 24, 2009 5:48 pm
Location: tunisia


Return to General Discussion

Who is online

Users browsing this forum: Google [Bot] and 64 guests