3

Im using a MVC in PHP and I have this script created in my form page to validate three text boxes. When these three text boxes contain a value my php code in my controller asks Google Map Api for the closest directions based on the input of these three fields.

In my script I have the variable "direccion" which is what I need to pass to the controller using PHP but im not sure how to accomplish this.

Script Code (View):

jQuery(document).ready(function () {

    var direccion="";
    var flag = false;
    jQuery(".validation").change(function () {
        flag = true;
        jQuery(".validation").each(function () {
            if (jQuery(this).val().trim() == "") {
                alert("false");
                flag = false;
            }
        });
        if (flag==true) {

            var calle = jQuery("#ff_elem295").val();
            var municipio = jQuery("#id_municipio option:selected").text();
            var provincia = jQuery("#id_provincia option:selected").text();             

            direccion = calle +","+ municipio +","+ provincia;
            direccion = direccion.replace(/\s/g,'+');
            //alert(direccion);
        }       
});

jQuery.ajax({
            url: "index.php?option=com_cstudomus&controller=saloninmobiliarios&task=calcularDistancias",
            data : direccion,
            dataType : 'html'
        }).done(function(){
                var data = data;
        });
});

PHP Code (Controller):

function calcularDistancias(){

    $valor = JRequest::getVar('direccion');

    $url =  'http://maps.googleapis.com/maps/api/geocode/json?address='. $valor .'&sensor=false';

    $data = file_get_contents($url);

    $data_array = json_decode($data,true);

    $lat = $data_array[results][0][geometry][location][lat];
    $lng = $data_array[results][0][geometry][location][lng];
......
}
2
  • I think you need to use ajax..? Commented Mar 4, 2014 at 12:15
  • Yes, thats what I am trying to do and where I pass the variable "direccion" but im not sure how to pick this variable up using PHP and using it in my php function Commented Mar 4, 2014 at 12:16

3 Answers 3

4

data property in the object passed to jQuery.ajax is an object.

data : { direccion: direccion }

Then you can access the value of direccion in your controller as a request parameter.

Sign up to request clarification or add additional context in comments.

4 Comments

He has to set type option to 'POST' as well if he want to pass data as POST
I remove the POST thing and wrote it more general
@RobinWebdev what do you refer to when you say request parameter? Like $_GET?
By default it is $_GET. If you want to use $_POST you have to set type options to POST like @hindmost said.
1

In the if condition put your ajax request like

if(flag == true) {
    jQuery.ajax({
        url: "index.php?option=com_cstudomus&controller=saloninmobiliarios&task=calcularDistancias",
        data : {direction : direccion},
        dataType : 'html'
    }).done(function(){
            var data = data;
    });
}

Comments

0

In addition the retrieved data are missing in your code, don't forget to put data in done function :

.done(function(){
    var data = data;
});

To

.done(function(data){
    var data = data;
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.