• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 554
  • Last Modified:

Do you know how to call a PHP program from a javascript code ?

Hi Experts!

Do you know how to call a PHP program from a javascript code ?

To complicate it's needed that the javascript has to be called outside html code...

Like that

Using KoolPHPSuite
//Button with Left Image
    $btnOK_left_image = new KoolButton("btnOK_left_image");
    $btnOK_left_image->Text = "Pesquisar";
    $btnOK_left_image->AutoPostback = true;
    $btnOK_left_image->LeftImage = $KoolControlsFolder .
        "/KoolForm/icons/plain/check2.png";
    $mybutton->OnClick = "acionar_2p";
    $myform_manager->AddControl($btnOK_left_image);

Open in new window


That traditional way apparently doesn't run in KoolPHPSuite the way I need...

<input type='button' value='Filtrar Lançamentos' onclick="javascript: location.href='Teste_Grid_Validator02.php';" /-->

Open in new window


The javascript code must call a PHP program when the button is actioned,

Thanks in advance!
0
Eduardo Fuerte
Asked:
Eduardo Fuerte
  • 22
  • 20
  • 6
1 Solution
 
Julian HansenCommented:
<input type='button' value='Filtrar Lançamentos' onclick="javascript: window.location='Teste_Grid_Validator02.php';" />

Open in new window

0
 
Eduardo FuerteAuthor Commented:
Hello

I need a javascript function to be called with the same effect... due other KoolPHPSuite controls using html buttons it's not a valid way...
0
 
Julian HansenCommented:
Ok but what do you want to do - you posted a button so that is the response you got.

When you say "call" a php program - do you mean load the page or make an AJAX call to the page and do something with the response?

I have no idea what you need to do here.
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
Ray PaseurCommented:
You can use jQuery to create a background call to a PHP script.  Any of a number of browser events can trigger the jQuery AJAX request.  Here is the hello-world example.
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Jquery/A_10712-The-Hello-World-Exercise-with-jQuery-and-PHP.html
0
 
Eduardo FuerteAuthor Commented:
What I mean is a javascript function with the same effect than submit a form but fired by a button created by a PHP class (KoolPHPSuite) instead of a HTML button.

I think Ray's reply pointed in that way ... but I don't have expertise in develop  jQuery  code.
But I'm searching how to...
0
 
Ray PaseurCommented:
If you can show us the SSCCE teaching how to create the button in KoolPHPSuite, we can probably show you how to modify the button to make an AJAX request.
0
 
Eduardo FuerteAuthor Commented:
Ray

reference:
http://demo.koolphp.net/Examples/KoolButton/Button/KoolButton/index.php


The KoolPHPSuite code:

<?php
...
  //Button with Left Image
    $btnOK_left_image = new KoolButton("btnOK_left_image");
    $btnOK_left_image->Text = "Pesquisar";
    $btnOK_left_image->AutoPostback = true;
    $btnOK_left_image->LeftImage = $KoolControlsFolder .
        "/KoolForm/icons/plain/check2.png";
    $mybutton->OnClick = "[b]acionar_2p"[/b];
    $myform_manager->AddControl($btnOK_left_image);
...
?>

Open in new window


Render:

<?php
     echo $btnOK_left_image->Render();
?> 

Open in new window



The javascript function acionar_2p must fire the php code the same way html button does..
0
 
Ray PaseurCommented:
Great -- we are getting closer. Can you please post a link to a web page so I can see the rendered HTML and JavaScript associated with the $btnOK_left_image object?
0
 
Eduardo FuerteAuthor Commented:
Ok
I'm just transfering KoolPHPSuite libraries to web host and then the pages where I've used the buttons for you to see how it works. But the needed javascript is not developed yet...
0
 
Ray PaseurCommented:
But how can we debug the HTML if we cannot see it?
0
 
Julian HansenCommented:
Still confused why you can't do this?

function acionar_2p()
{
   window.location = "theurlyouwantogoto.php";
}

Open in new window

0
 
Julian HansenCommented:
If you want to call a page (it is irrellevant whether it is php, html or some other page) from a button click all you need to do is either put the line

window.location = url;

In the onclick event OR point onclick to a function that does the above.
0
 
Julian HansenCommented:
If you attempt the above and it does not work - please provide a link to it so we can see what you have done. In terms of what you have asked - this is the correct solution so if it is not working we need to see why not.
0
 
Ray PaseurCommented:
please provide a link to it so we can see what you have done
Amen!
0
 
Eduardo FuerteAuthor Commented:
I'm finally back....

Here is the link to follow...

http://www.espiriplug.com.br/Espiriplug_prj/Kool_Teste/Teste_Kool_Menu01_EE.php

Then use this option:
img001
And then
img002
The .php code I've used with some little explanations:

Code-php-EE-200613.pdf

If something more is needed please tell me.
0
 
Ray PaseurCommented:
This may be more work than it's worth to try to fix.  Please see:
http://validator.w3.org/check?uri=http%3A%2F%2Fwww.laprbass.com%2FRAY_temp_efuerte.php&charset=%28detect+automatically%29&doctype=Inline&group=0

Here is the document I fed to the validator (with newlines to make it easier to read).
<html>
<head></head>
<body>
<!-- Step 5: Render menu -->
        
<!--KoolMenu version 1.2.0.0 - www.koolphp.net -->
<script type='text/javascript'>
if (document.getElementById('__defaultKMU')==null)
{
   var _head = document.getElementsByTagName('head')[0];
   var _link = document.createElement('link'); 
   _link.id = '__defaultKMU';
   _link.rel='stylesheet'; 
   _link.href='/KoolPHPSuite/KoolControls/KoolMenu/styles/default/default.css';
   _head.appendChild(_link);
}
</script>

<div id='km' class='defaultKMU' style='z-index:5000;'>
<ul class='kmuRootGroup kmuHorizontal'>
<li id='file' class='kmuItem  kmuFirst' >
<a class='kmuLink '  title=''   > <span class='kmuText kmuExpandDown'>Controles</span>
</a>
<div class='kmuSlide kmuPrem' style='display:none;overflow:hidden;z-index:3'>
<ul id='file_group' class='kmuGroup kmuVertical kmuLevel1'>
<li id='new' class='kmuItem  kmuLast' >
<a class='kmuLink '  title=''   > <span class='kmuText kmuExpandRight'>Extratos...</span>
</a>
<div class='kmuSlide kmuPrem' style='display:none;overflow:hidden;z-index:6'>
<ul id='new_group' class='kmuGroup kmuVertical kmuLevel2'>
<li id='newfile' class='kmuItem  kmuFirst' >
<a class='kmuLink ' href='Teste_Kool_Date_Picker01.php' title=''   > <span class='kmuText '>Conta Corrente/ Caixa</span>
</a>
<input id='newfile_setting' type='hidden' value='{"OffsetX":0,"OffsetY":0,"ExpandDirection":"Auto"}' autocomplete='off' />
</li>
<li id='newfolder' class='kmuItem  kmuLast' >
<a class='kmuLink ' href='Teste_Kool_Perifil2_01.php' title=''   > <span class='kmuText '>Pesquisa de Lancamentos</span>
</a>
<input id='newfolder_setting' type='hidden' value='{"OffsetX":0,"OffsetY":0,"ExpandDirection":"Auto"}' autocomplete='off' />
</li>
</ul>
</div>
<input id='new_setting' type='hidden' value='{"OffsetX":0,"OffsetY":0,"ExpandDirection":"Auto"}' autocomplete='off' />
</li>
</ul>
</div>
<input id='file_setting' type='hidden' value='{"OffsetX":0,"OffsetY":0,"ExpandDirection":"Auto"}' autocomplete='off' />
</li>
<li id='new1' class='kmuItem  ' >
<a class='kmuLink '  title=''   > <span class='kmuText kmuExpandDown'>Lan?amentos Financeiros</span>
</a>
<div class='kmuSlide kmuPrem' style='display:none;overflow:hidden;z-index:3'>
<ul id='new1_group' class='kmuGroup kmuVertical kmuLevel1'>
<li id='newfolder' class='kmuItem  kmuFirst' >
<a class='kmuLink ' href='Teste_Form_Validator01_EE.php' title=''   > <span class='kmuText '>Inclus?o</span>
</a>
<input id='newfolder_setting' type='hidden' value='{"OffsetX":0,"OffsetY":0,"ExpandDirection":"Auto"}' autocomplete='off' />
</li>
<li id='newfolder' class='kmuItem  kmuLast' >
<a class='kmuLink ' href='T_K_Date_Picker01_02_EE.php' title=''   > <span class='kmuText '>Manuten??es</span>
</a>
<input id='newfolder_setting' type='hidden' value='{"OffsetX":0,"OffsetY":0,"ExpandDirection":"Auto"}' autocomplete='off' />
</li>
</ul>
</div>
<input id='new1_setting' type='hidden' value='{"OffsetX":0,"OffsetY":0,"ExpandDirection":"Auto"}' autocomplete='off' />
</li>
<li id='teste' class='kmuItem  ' >
<a class='kmuLink '  title=''   > <span class='kmuText kmuExpandDown'>Teste</span>
</a>
<div class='kmuSlide kmuPrem' style='display:none;overflow:hidden;z-index:3'>
<ul id='teste_group' class='kmuGroup kmuVertical kmuLevel1'>
<li id='teste1' class='kmuItem  kmuLast' >
<a class='kmuLink '  title=''   > <span class='kmuText kmuExpandRight'>teste1</span>
</a>
<div class='kmuSlide kmuPrem' style='display:none;overflow:hidden;z-index:6'>
<ul id='teste1_group' class='kmuGroup kmuVertical kmuLevel2'>
<li id='teste2' class='kmuItem  kmuLast' >
<a class='kmuLink '  title=''   > <span class='kmuText kmuExpandRight'>Teste2</span>
</a>
<div class='kmuSlide kmuPrem' style='display:none;overflow:hidden;z-index:9'>
<ul id='teste2_group' class='kmuGroup kmuVertical kmuLevel3'><li id='teste3' class='kmuItem  kmuLast' >
<a class='kmuLink '  title=''   > <span class='kmuText '>Folder</span>
</a>
<input id='teste3_setting' type='hidden' value='{"OffsetX":0,"OffsetY":0,"ExpandDirection":"Auto"}' autocomplete='off' />
</li>
</ul>
</div>
<input id='teste2_setting' type='hidden' value='{"OffsetX":0,"OffsetY":0,"ExpandDirection":"Auto"}' autocomplete='off' />
</li>
</ul>
</div>
<input id='teste1_setting' type='hidden' value='{"OffsetX":0,"OffsetY":0,"ExpandDirection":"Auto"}' autocomplete='off' />
</li>
</ul>
</div>
<input id='teste_setting' type='hidden' value='{"OffsetX":0,"OffsetY":0,"ExpandDirection":"Auto"}' autocomplete='off' />
</li>
<li id='file' class='kmuItem  kmuLast' >
<a class='kmuLink '  title=''   > <span class='kmuText kmuExpandDown'>File</span>
</a>
<div class='kmuSlide kmuPrem' style='display:none;overflow:hidden;z-index:3'>
<ul id='file_group' class='kmuGroup kmuVertical kmuLevel1'>
<li id='new' class='kmuItem  kmuLast' >
<a class='kmuLink '  title=''   > <span class='kmuText kmuExpandRight'>New...</span>
</a>
<div class='kmuSlide kmuPrem' style='display:none;overflow:hidden;z-index:6'>
<ul id='new_group' class='kmuGroup kmuVertical kmuLevel2'>
<li id='newfile' class='kmuItem  kmuFirst' >
<a class='kmuLink '  title=''   > <span class='kmuText '>File</span>
</a>
<input id='newfile_setting' type='hidden' value='{"OffsetX":0,"OffsetY":0,"ExpandDirection":"Auto"}' autocomplete='off' />
</li>
<li id='newfolder' class='kmuItem  kmuLast' >
<a class='kmuLink '  title=''   > <span class='kmuText '>Folder</span>
</a>
<input id='newfolder_setting' type='hidden' value='{"OffsetX":0,"OffsetY":0,"ExpandDirection":"Auto"}' autocomplete='off' />
</li>
</ul>
</div>
<input id='new_setting' type='hidden' value='{"OffsetX":0,"OffsetY":0,"ExpandDirection":"Auto"}' autocomplete='off' />
</li>
</ul>
</div>
<input id='file_setting' type='hidden' value='{"OffsetX":0,"OffsetY":0,"ExpandDirection":"Auto"}' autocomplete='off' />
</li>
</ul>  
<input id='km_setting' type='hidden' value='{"ExpandDelay":210,"CollapseDelay":210,"ClickToOpen":false,"ExpandAnimation":{"Duration":200,"Type":"EaseBoth"},"CollapseAnimation":{"Duration":200,"Type":"EaseBoth"},"PostBackOnSelect":false,"ContextMenu":false}' autocomplete='off' /> 
<input id='km_select' name='km_select' type='hidden' autocomplete='off' />
</div>
<script type='text/javascript'>
if(typeof _libKMU=='undefined')
{
   document.write(unescape("%3Cscript type='text/javascript' src='/KoolPHPSuite/KoolControls/KoolMenu/koolmenu.php?32981a13284db7a021131df49e6cd203'%3E %3C/script%3E"));
   _libKMU=1;
}
</script>
<script type='text/javascript'>
var km; 
function km_init()
{ 
   km = new KoolMenu('km');
}
if (typeof(KoolMenu)=='function')
{
   km_init();
}else
{
   if(typeof(__KMUInits)=='undefined')
   {
      __KMUInits=new Array();
   } __KMUInits.push(km_init);
   if(typeof(_libKMU)=='undefined')
   {
      var _head = document.getElementsByTagName('head')[0];
      var _script = document.createElement('script');
      _script.type='text/javascript';
      _script.src='/KoolPHPSuite/KoolControls/KoolMenu/koolmenu.php?32981a13284db7a021131df49e6cd203'; 
      _head.appendChild(_script);
      _libKMU=1;
   }
}
</script>
</body>
</html>

Open in new window

Not sure where to go next with this.
0
 
Eduardo FuerteAuthor Commented:
I really don't understand what you did.
It only opens a non functional menu, I had no problems with menu.
By the way in the meanwhile I've found a solution without the non funcional button.
0
 
Julian HansenCommented:
You have some major issues with your code though.

Your JQuery include and the acionar_2p function are both defined after the </html> tag.

There are 73 warnings on the page

Although acionar_2p is being defined - it is not called anywhere - the ValidateForm function is firing when you click that button.

Unfortunately the code you posted was no use because you put it in a PDF file with line numbers - and to extract from there is too much effort.

Please repost your code using either an attached file or inside the code tags provided.
0
 
Eduardo FuerteAuthor Commented:
Hello JulianH

Thank you for your efforts until now!

Here´s the code.

<?php

    include ('Teste_Kool_Menu01_EE.php');
    include ('config.php');
    include ('db.php');

    date_default_timezone_set('America/Sao_Paulo');

    $KoolControlsFolder = "../../KoolPHPSuite/KoolControls"; //Relative path to "KoolPHPSuite/KoolControls" folder
    require $KoolControlsFolder . "/KoolForm/koolform.php";
    require $KoolControlsFolder . "/KoolComboBox/koolcombobox.php";
    require $KoolControlsFolder . "/KoolCalendar/koolcalendar.php";

    $myform_manager = new KoolForm("form1");
    $myform_manager->scriptFolder = $KoolControlsFolder . "/KoolForm";
    $myform_manager->styleFolder = "sunset";

    // KoolPHPSuite validator
    $validator = $myform_manager->AddControl(new KoolCustomValidator("valiadtor"));
    $validator->ErrorMessage = "Observe as mensagens de erro.";
    $validator->ClientValidationFunction = "validateForm";

    $form_decoration_enabled = "1";

    $datepicker = new KoolDatePicker("datepicker"); //Create calendar object
    $datepicker->scriptFolder = $KoolControlsFolder . "/KoolCalendar";
    $datepicker->styleFolder = "default";
    $datepicker->DateFormat = "d/m/Y";
    //$datepicker->Date = date();

    $datepicker1 = new KoolDatePicker("datepicker1"); //Create calendar object
    $datepicker1->scriptFolder = $KoolControlsFolder . "/KoolCalendar";
    $datepicker1->styleFolder = "default";
    $datepicker1->DateFormat = "d/m/Y";

     //Button with Left Image
    $btnOK_left_image = new KoolButton("btnOK_left_image");
    $btnOK_left_image->Text = "Pesquisar";
    $btnOK_left_image->AutoPostback = true;
    $btnOK_left_image->LeftImage = $KoolControlsFolder .
        "/KoolForm/icons/plain/check2.png";
    // The button that fires the function I need
    $mybutton->OnClick = "acionar_2p";
    $myform_manager->AddControl($btnOK_left_image);

    $datepicker->Init();
    $datepicker1->Init();

    if (isset($_POST["form_decoration_enabled"])) {
        $form_decoration_enabled = $_POST["form_decoration_enabled"];
    }

    $selectStyle = "sunset";

    //Set DecorationEnabled to enable/disable form decoration
    $myform_manager->DecorationEnabled = ($form_decoration_enabled == "1") ? true : false;
    //Set the style for the form
    $myform_manager->styleFolder = $selectStyle;

    //The form need to be init.
    $myform_manager->Init();

?>

<html>
<head>
</head>

<body>


<!-- OK(traditional approicah) form id="form1" method="post" action="Teste_Grid_Validator02.php" onSubmit="return validateForm()"-->
<form id="form1" method="post">

<br/>
<h3>
Filtro de Lançamentos para Manutenção
</h3>

<?php
   echo $btnOK_left_image->Render();
?> 

	<h5>Tipo de Lançamento:</h5>
    <hr />
    <input id="radio1" type="radio" name="tp_lanc"  checked /><label for="radio1">Despesa</label>
    <input id="radio2" type="radio" name="tp_lanc" /><label for="radio2">Receita</label>
    <input id="radio3" type="radio" name="tp_lanc" /><label for="radio3">Transferencia</label>
		
<br />
<br />

CC/ Cx.:
<select name="sb_cccx">
<option selected="selected">     
                   
</option>
                    
 <?php

    //----------Inicio Combo 1 (CR) -----------------------------------------------------------------
    $sql = mysql_query("select sb_cccx, cx_info, substr(descricao,1,25) as descricao from cccx where ativa=1 order by descricao");
    //----------Final Combo 1-------------------------------------------------------------------------

    while ($row = mysql_fetch_array($sql)) {
        $id = $row['sb_cccx'];
        $data = $row['descricao'];
        echo '<option value="' . $id . '">' . $data . '</option>';
    }

?>
 </select>

	<div style="padding-top:20px;padding-bottom:40px;width:650px;">
		Data Inicial:

<?php

    echo $datepicker->Render();

?>

		&nbsp;&nbsp;&nbsp;&nbsp;Data Final:
		
<?php

    echo $datepicker1->Render();

?>

<br />
<br />
CR (L):
<select name="sb_cr">
<option selected="selected">                
</option>
<?php

    //----------Inicio Combo 1 (CR) -----------------------------------------------------------------
    $sql = mysql_query("select sb_cr, cr_info, substr(descricao,1,25) as descricao from cr order by descricao");
    //----------Final Combo 1-------------------------------------------------------------------------

    while ($row = mysql_fetch_array($sql)) {
        $id = $row['sb_cr'];
        $data = $row['descricao'];
        echo '<option value="' . $id . '">' . $data . '</option>';
    }

?>
</select>

<br/>
<br />

Desp (H) :
<select name="country" class="country">
<option selected="selected">                
</option>

<?php

    // Ajustar
    $nivel = 1;

    //----------Inicio Combo 2 (DSGER Despesa Hierárquica)--------------------------------------------
    // include ('db.php');
    $sql = mysql_query("SELECT SB_DGE as SB_DGE_PAI, DESCRICAO as DESC_PAI FROM DSGER d WHERE d.TIPO=2 and NIVEL='$nivel';");

    while ($row = mysql_fetch_array($sql)) {
        $id = $row['SB_DGE_PAI'];
        $data = $row['DESC_PAI'];
        echo '<option value="' . $id . '">' . $data . '</option>';
    }

?>
</select> 
<br/>
<br/>
Desp (L) :
<select name="city" class="city">
<option selected="selected">            </option>
</select>
<br />
<br />


<?php
   echo $validator->Render();
?>

<?php
   echo $myform_manager->Render();
?>


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

<?php

    if ($myform_manager->IsPostBack) {
        echo "<div style='margin-bottom:5px;font-size:bold;'>Form is posted sucessfully!</div>";

        // I guess here is the place to call 'Teste_Grid_Validator02.php'  but don't work!
        // So I've decided to put a button to do the job - no effect
        echo "javascript: location.href='Teste_Grid_Validator02.php';";
    }
    else
    {
        echo "<div style='margin-bottom:5px;font-size:bold;'>Form is NOT posted yet!</div>";        
    }

?>

</form>

</body>
</html>

<script type="text/javascript">

function validateForm()
{

var y7=datepicker.get_value();

if (y7.length==0)
{
   alert("Preencha obrigatoriamente a data inicial!");
   return false;
}

var y6=datepicker1.get_value();

if (y6.length==0)
{
   alert("Preencha obrigatoriamente a data final!");
   return false;
}

var y8=document.forms["form1"]["sb_cccx"].value;

if (y8.length==0)
{
   alert("Preencha obrigatoriamente Conta Corrente/ Caixa!");
   return false;
}
    // Another tentative!
    //location.href='Teste_Grid_Validator02.php';
}

</script>	

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$(".country").change(function()
{
var id=$(this).val();
var dataString = 'id='+ id;
$.ajax
({
type: "POST",
url: "ajax_city.php",
data: dataString,
cache: false,
success: function(html)
{
$(".city").html(html);
} 
});
});
});
</script>
<style>
label
{
font-weight:bold;
padding:10px;
}
</style>

</form>
</body>
</html>


<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
function acionar_2p()
{
  // The function fired by the button  
   window.location = "Teste_Grid_Validator02.php";
}
</script>

Open in new window


The button was a tentative to call another  php program since

if ($myform_manager->IsPostBack) {... didn't work out...


If something else is needed please let me know!
0
 
Julian HansenCommented:
On line 39 try changing

$btnOK_left_image->AutoPostback = true;

To

$btnOK_left_image->AutoPostback = false;

Also move your script lines 288 onwards inside the <body> tags.
0
 
Eduardo FuerteAuthor Commented:
I've tryed you've pointed with no success.

Then I've workarounded by changing the code for not use KoolPHPSuite validator
<?php
   //echo $validator->Render();
?>

Open in new window


 so the form is posted...

(and removed the button "Pesquisar" that has no effect)


I've just put this

<form id="form1" method="post" action="Teste_Grid_Validator02.php" onsubmit="return validateForm()" >

Open in new window


The form is posted and the Teste_Grid_Validator02.php is fired....
(Maybe its the only possible way to call another page)

I couldn't use KoolPHPSuite validator but it desn't make to many diference.

By now I guess it's the possible solution...
0
 
Julian HansenCommented:
Maybe its the only possible way to call another page
This does not make sense.

Calling a page is simply putting a URL on the page and causing the browser to act on that URL.

An <a> element does it by default - but you can do it on any element or action using

window.location
0
 
Eduardo FuerteAuthor Commented:
Ok, using an anchor is possible too, but I'm refering in how to deal with a situation like that ?
(considering not using form action...)
<?php

    if ($myform_manager->IsPostBack) {
        echo "<div style='margin-bottom:5px;font-size:bold;'>Form is posted sucessfully!</div>";

        // I guess here is the place to call 'Teste_Grid_Validator02.php'  but don't work!
        // So I've decided to put a button to do the job - no effect
        echo "javascript: location.href='Teste_Grid_Validator02.php';";
    }
    else
    {
        echo "<div style='margin-bottom:5px;font-size:bold;'>Form is NOT posted yet!</div>";        
    }

?>

Open in new window

0
 
Julian HansenCommented:
Wait a bit - looks like you want a redirect from the server side something liek this

<?php

    if ($myform_manager->IsPostBack) {
        echo "<div style='margin-bottom:5px;font-size:bold;'>Form is posted sucessfully!</div>";

        // I guess here is the place to call 'Teste_Grid_Validator02.php'  but don't work!
        // So I've decided to put a button to do the job - no effect
       header('location: Teste_Grid_Validator02.php');
    }
    else
    {
        echo "<div style='margin-bottom:5px;font-size:bold;'>Form is NOT posted yet!</div>";        
    }

?>

Open in new window

That will cause the page to redirect to the required page.
0
 
Eduardo FuerteAuthor Commented:
Hello JulianH

You're probably going in a correct way but this error occurs
img01
http://www.espiriplug.com.br/Espiriplug_prj/Kool_Teste/T_K_Date_Picker01_02_EE.php

<?php

    if ($myform_manager->IsPostBack) {
        echo "<div style='margin-bottom:5px;font-size:bold;'>Form is posted sucessfully!</div>";

        // I guess here is the place to call 'Teste_Grid_Validator02.php'  but don't work!
        // So I've decided to put a button to do the job - no effect
        //echo "javascript: location.href='Teste_Grid_Validator02.php';";
        
        header('location: Teste_Grid_Validator02.php');
        
    }
    else
    {
        echo "<div style='margin-bottom:5px;font-size:bold;'>Form is NOT posted yet!</div>";        
    }

?>

Open in new window


img02
0
 
Julian HansenCommented:
It won't because of the echo that happens before that.

The header redirect only works if no output has been sent to the browser. So you probably want this
    // MAKE SURE THERE IS NO OUTPUT BEFORE THIS
    if ($myform_manager->IsPostBack) {

        // I guess here is the place to call 'Teste_Grid_Validator02.php'  but don't work!
        // So I've decided to put a button to do the job - no effect
        //echo "javascript: location.href='Teste_Grid_Validator02.php';";
        
        header('location: Teste_Grid_Validator02.php');
        
    }
 

Open in new window

0
 
Eduardo FuerteAuthor Commented:
Unfortunatelly, still not...
img01
0
 
Julian HansenCommented:
All the information you need is in the warning.

As I said before - you cannot header redirect if you have already sent output to the browser.

On line 209 you are outputting something that is breaking the header redirect.
0
 
Eduardo FuerteAuthor Commented:
It's only a "number line" coincidence.
I've comment the "echo..."
It's could be easily concluded  since the phrase:  Form is posted successfully! isn't presented....
img-EE-04-300613-01.png
0
 
Eduardo FuerteAuthor Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for EFuerte's comment #a39286805

for the following reason:

Since I've found a workaround by myself and the replies are unproductive.
0
 
Julian HansenCommented:
Since I've found a workaround by myself and the replies are unproductive.
Unproductive?????

And what was your workaround? And after showing your workaround please explain how you arrived at it in a way that had nothing to do with the comments made in this thread?

I strongly object to this.
0
 
Eduardo FuerteAuthor Commented:
Hello JulianH


Looks like you got angry, if so it's not the case.

See above my interaction:

I've tryed what you've pointed with no success.

Then I've workarounded by changing the code for not use KoolPHPSuite validator

<?php
   //echo $validator->Render();
?> 

Open in new window


 so the form is posted...

(and removed the button "Pesquisar" that has no effect)

I've just put this:

<form id="form1" method="post" action="Teste_Grid_Validator02.php" onsubmit="return validateForm()/>

Open in new window


Then you've suggested to use

header('location: Teste_Grid_Validator02.php');

That has no effects.

You've been informed of that and after some interactions I get no other replies. So I had to use my workaround.
If you could evidence that your solution works I could reconsider with no problems.
0
 
Julian HansenCommented:
That has no effects.

Because ... you are doing an output somewhere  before the header statement.

You need to look at your entire script before the header statement and see what is sending output to the browser.

Once that has been sorted the header statement will work.

In your second to last post you posted a PNG showing you had commented out line 203 - fine but that is not where the problem is - there is another line somewhere in your code sending output to the browser - that is what you have to find.
0
 
Eduardo FuerteAuthor Commented:
Ok, but accordingly to what was posted right from the start KoolPHPSuite is used, evidenced by pictures with Koolmenu and KoolCalendar that was posted, so, obvously, that causes outputs!
That confirm your is not a valid solution in this case, doesn't it?
0
 
Julian HansenCommented:
Nope - because you can put an

ob_start

before the KoolMenu code

and then use ob_get_clean() to get he contents and output them after the header statement.

So ...

ob_start();
....
// KoolMenu stuff here

header( ... ); // Header redirect here
...
echo ob_get_clean(); // Start sending output to browser.
0
 
Eduardo FuerteAuthor Commented:
It still doesn't work

link:

http://www.espiriplug.com.br/Espiriplug_prj/Kool_Teste/T_K_Date_Picker01_02_EE.php


Could you please check  in the code  if is correct placed?

<?php

    ob_start();
    
    include ('Teste_Kool_Menu01_EE.php');
    include ('config.php');
    include ('db.php');

    date_default_timezone_set('America/Sao_Paulo');

    $KoolControlsFolder = "../../KoolPHPSuite/KoolControls"; //Relative path to "KoolPHPSuite/KoolControls" folder
    require $KoolControlsFolder . "/KoolForm/koolform.php";
    require $KoolControlsFolder . "/KoolComboBox/koolcombobox.php";
    require $KoolControlsFolder . "/KoolCalendar/koolcalendar.php";

    $myform_manager = new KoolForm("form1");
    $myform_manager->scriptFolder = $KoolControlsFolder . "/KoolForm";
    $myform_manager->DecorationEnabled = true;
	$myform_manager->styleFolder = "sunset";

    // KoolPHPSuite validator
    $validator = $myform_manager->AddControl(new KoolCustomValidator("valiadtor"));
    $validator->ErrorMessage = "Observe as mensagens de erro.";
    $validator->ClientValidationFunction = "validateForm";

    $form_decoration_enabled = "1";

    $datepicker = new KoolDatePicker("datepicker"); //Create calendar object
    $datepicker->scriptFolder = $KoolControlsFolder . "/KoolCalendar";
    $datepicker->styleFolder = "default";
    $datepicker->DateFormat = "d/m/Y";
    //$datepicker->Date = date();

    $datepicker1 = new KoolDatePicker("datepicker1"); //Create calendar object
    $datepicker1->scriptFolder = $KoolControlsFolder . "/KoolCalendar";
    $datepicker1->styleFolder = "default";
    $datepicker1->DateFormat = "d/m/Y";

     //Button with Left Image
    $btnOK_left_image = new KoolButton("btnOK_left_image");
    $btnOK_left_image->Text = "Pesquisar";
    $btnOK_left_image->AutoPostback = true;
    $btnOK_left_image->LeftImage = $KoolControlsFolder .
        "/KoolForm/icons/plain/check2.png";
    // The button that fires the function I need
    $mybutton->OnClick = "acionar_2p";
    $myform_manager->AddControl($btnOK_left_image);

    $datepicker->Init();
    $datepicker1->Init();

    if (isset($_POST["form_decoration_enabled"])) {
        $form_decoration_enabled = $_POST["form_decoration_enabled"];
    }

    $selectStyle = "sunset";

    //Set DecorationEnabled to enable/disable form decoration
    $myform_manager->DecorationEnabled = ($form_decoration_enabled == "1") ? true : false;
    //Set the style for the form
    $myform_manager->styleFolder = $selectStyle;

    //The form need to be init.
    $myform_manager->Init();

?>

<html>
<head>
</head>

<body>


<!-- OK(traditional approicah) form id="form1" method="post" action="Teste_Grid_Validator02.php" onSubmit="return validateForm()"-->
<form id="myform" method="post" >

<br/>
<h3>
Filtro de Lançamentos para Manutenção
</h3>



	<h5>Tipo de Lançamento:</h5>
    <hr />
    <input id="radio1" type="radio" name="tp_lanc"  checked /><label for="radio1">Despesa</label>
    <input id="radio2" type="radio" name="tp_lanc" /><label for="radio2">Receita</label>
    <input id="radio3" type="radio" name="tp_lanc" /><label for="radio3">Transferencia</label>
		
<br />
<br />

CC/ Cx.:
<select name="sb_cccx">
<option selected="selected">     
                   
</option>
                    
 <?php

    //----------Inicio Combo 1 (CR) -----------------------------------------------------------------
    $sql = mysql_query("select sb_cccx, cx_info, substr(descricao,1,25) as descricao from cccx where ativa=1 order by descricao");
    //----------Final Combo 1-------------------------------------------------------------------------

    while ($row = mysql_fetch_array($sql)) {
        $id = $row['sb_cccx'];
        $data = $row['descricao'];
        echo '<option value="' . $id . '">' . $data . '</option>';
    }

?>
 </select>

	<div style="padding-top:20px;padding-bottom:40px;width:650px;">
		Data Inicial:

<?php

    echo $datepicker->Render();

?>

		&nbsp;&nbsp;&nbsp;&nbsp;Data Final:
		
<?php

    echo $datepicker1->Render();

?>

<br />
<br />
CR (L):
<select name="sb_cr">
<option selected="selected">                
</option>
<?php

    //----------Inicio Combo 1 (CR) -----------------------------------------------------------------
    $sql = mysql_query("select sb_cr, cr_info, substr(descricao,1,25) as descricao from cr order by descricao");
    //----------Final Combo 1-------------------------------------------------------------------------

    while ($row = mysql_fetch_array($sql)) {
        $id = $row['sb_cr'];
        $data = $row['descricao'];
        echo '<option value="' . $id . '">' . $data . '</option>';
    }

?>
</select>

<br/>
<br />

Desp (H) :
<select name="country" class="country">
<option selected="selected">                
</option>

<?php

    // Ajustar
    $nivel = 1;

    //----------Inicio Combo 2 (DSGER Despesa Hierárquica)--------------------------------------------
    // include ('db.php');
    $sql = mysql_query("SELECT SB_DGE as SB_DGE_PAI, DESCRICAO as DESC_PAI FROM DSGER d WHERE d.TIPO=2 and NIVEL='$nivel';");

    while ($row = mysql_fetch_array($sql)) {
        $id = $row['SB_DGE_PAI'];
        $data = $row['DESC_PAI'];
        echo '<option value="' . $id . '">' . $data . '</option>';
    }

?>
</select> 
<br/>
<br/>
Desp (L) :
<select name="city" class="city">
<option selected="selected">            </option>
</select>
<br />
<br />

<?php
   echo $validator->Render();
?>

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

<?php

    if ($myform_manager->IsPostBack) {
        //echo "<div style='margin-bottom:5px;font-size:bold;'>Form is posted sucessfully!</div>";

        // I guess here is the place to call 'Teste_Grid_Validator02.php'  but don't work!
        // So I've decided to put a button to do the job - no effect
            //echo "javascript: location.href='Teste_Grid_Validator02.php';";
        header('location: Teste_Grid_Validator02.php');
        echo ob_get_clean();
    }
    else
    {
        echo "<div style='margin-bottom:5px;font-size:bold;'>Form is NOT posted yet!</div>";        
    }

?>

<?php
   echo $myform_manager->Render();
?>


<?php
   //echo $btnOK_left_image->Render();
?> 



</form>

</body>
</html>

<script type="text/javascript">

function validateForm()
{

var y7=datepicker.get_value();

if (y7.length==0)
{
   alert("Preencha obrigatoriamente a data inicial!");
   return false;
}

var y6=datepicker1.get_value();

if (y6.length==0)
{
   alert("Preencha obrigatoriamente a data final!");
   return false;
}

var y8=document.forms["form1"]["sb_cccx"].value;

if (y8.length==0)
{
   alert("Preencha obrigatoriamente Conta Corrente/ Caixa!");
   return false;
}
    // Another tentative!
    //location.href='Teste_Grid_Validator02.php';
}

</script>	

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$(".country").change(function()
{
var id=$(this).val();
var dataString = 'id='+ id;
$.ajax
({
type: "POST",
url: "ajax_city.php",
data: dataString,
cache: false,
success: function(html)
{
$(".city").html(html);
} 
});
});
});
</script>
<style>
label
{
font-weight:bold;
padding:10px;
}
</style>

</form>

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
function acionar_2p()
{
  // The function fired by the button  
   window.location = "Teste_Grid_Validator02.php";
}
</script>

</body>
</html>

Open in new window

0
 
Julian HansenCommented:
A couple of things

1. The echo ob_get_clean is in the wrong place - it needs to be outside of the if / else - put it on line 208.

2. Why are you doing the IsPostBack check on line 195 only?

I am not sure I am understanding what it is you are trying to do.

In your original post you wanted to redirect to another page when you click a button.

From there it seemed you wanted to redirect to another page after posting back?

The usual approach is this

1. Create form
2. Point form back to script that created it
3. When form is submitted - validate
4. If valid - perform any operations on the submitted data and then redirect to next page
5. If form data is not valid generate any error messages and enter form rendering code filling in posted data and error messages as appropriate.

usually 1-4 happens at the very start of the script - so no output is required before the redirect. You seem to have the redirect in the middle of your html output which is not ideal.

Please can you clarify if the above process is the one you are needing to follow.

Another scenario also occurred to me - might be a communication breakdown on my part. With this script are you still wanting something on the page that can redirect you - or are you wanting an automatic redirect after successful processing of the form?
0
 
Eduardo FuerteAuthor Commented:
Hello

My first goal was to use KoolPHPSuite features: form / controls
and then validate form controls by using  their validator .
Form: T_K_Date_Picker01_02_EE.php

After the form is validated  and submited another form need to be presented:
Form: Teste_Grid_Validator02.php

I've  follwed the examples furnished by them on their site.

The first form is validated but after that the second form isn't called whatever we could do after postback.

Using a button to fire javascript code to fire the second form was a initial paliative, when I've started question.

I've asked KoolPHPSuite assistance but they didn't reply in this case.

So I've asked EE assistance.

After that I've stopped using their validator and post the form at conventional way (my workaround).

By the way I've follow your suggestions and until now we don't have success, but it is valid as knowledge.

Since KoolPHPSuite could be a "black box" in some cases I prefer do not to use this specific features.

I really don't know if I'm conducting it in a misguided way, since I don't have too much expertise in PHP. But now I recognize your efforts in help me.
0
 
Julian HansenCommented:
Ok. then what you are wanting is a redirect after succesful processing.

I am out for a couple of days but can pick this up next week if you have not solved it by then.

Do a follow up post next week and I will take a look.
0
 
Eduardo FuerteAuthor Commented:
Ok! Have a nice 4th. July holyday ! (I guess....)

Next week I'm following...

And thanks for the continually assistance.
0
 
Julian HansenCommented:
No problem - and no not 4th of July - wrong continent.
0
 
Eduardo FuerteAuthor Commented:
Ok!
0
 
Eduardo FuerteAuthor Commented:
Hello

The solution was given here by gr8gonzoPosted on 2013-07-09 at 16:38:34ID: 39312716

"Please also read this article that I wrote today on this topic:"

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11997-How-to-Make-Javascript-Talk-to-PHP.html#c91872
0
 
Julian HansenCommented:
That deals with AJAX calls - which is not how I understood your question.

With AJAX when you make the call to the backend script typically you do not leave the page from where the call is made - if this was your requirement then there was a miscommunication in your question and how it was phrased.

I understood that on some action on a page you wanted to redirect to a completely different page.

The question here is - if you do use AJAX what do you want to happen on the originating page once the call has been completed? Do you want to stay on the same page and display some result? What action will then result in you navigating away from the page?
0
 
Eduardo FuerteAuthor Commented:
Really.

The question assumed another conotation when in progress.

The original purpose of calling a PHP program from javascript was to fire a MySQL stored  procedure by means of that PHP program.

Sorry, I had no idea of the jQuery possibilities in doing that without to load another form...(just with a non visual procedure). So I tought getting another page only to fire the sp and then to navigate back to the page that called it (by an anchor f.e.).
0
 
Julian HansenCommented:
Ok so you wanted to execute something on the backend - without leaving the page you were on - is that correct?

In which case AJAX is the way to go.

If you want to process the page you are on then redirect to another page then you need to use server side redirection - you can also do it with ajax but it sometimes more complex and unnecessary to do it that way.

To AJAX a page - there are a couple of things you need to consider

1. Are you going to need to return anything from the call and if so what do you need to do with that data (for example displaying a response message or adding HTML to the page)

2. What do you need to pass to the script.

The easiset way if you need a response that is plain html is the .load call

$('#element').load('nameofscript.php');

or if you need to pass data to the script then

$('#element').load('nameofscript.php',{param1: 'data',param2: 'more data');

This will call the script and load the return value into the specified element.

If you need a bit more control you can use the .post or .ajax calls.

Usually .post is sufficient for what you need - .ajax has more options but essentially does the same thing.

// on some event
$.post('nameofscript.php', {// data here}, function(response) {
   // do something with the response.
},'json');

The json parameter at the end is optional and basically tells .post that the data that is being returned is in JSON format - .post then decodes the JSON back into javascript objects and arrays that you can then use to do whatever you need to.

If you have form data you need to send back to the script you can use the .serialize call to get all the form variables into a string to use in the post - which allows you to AJAX a form exactly as if you posted it normally.

var data = $('#idofform').serialize();
$.post('nameofscript.php', data ...)

That's the gist of it.

Still not 100% sure though if this is what you want to do.
0
 
Eduardo FuerteAuthor Commented:
JulianH

You've pointed me the ways that certainly will attend my development.
Thank you for this so well elaborated summary.
Now is up to me to follow these contents. It's like a lesson!
0
 
Julian HansenCommented:
You are most welcome - thanks for the points.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 22
  • 20
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now