How to send current language with jquery ajax to php script?

For a multilingual website I am using translation files which I have included in the different pages. The different translation files look similar to this:


<?php
$name       = "name";
$email        = "email";
$tel             = "telephone";
$sub           = "subject";
$mes          = "message";
$btn            = "send";

$salutation  = "Dear";
$message   = "
Your message has been successfully sent and we will contact you as soon as possible If you have not received a response from us within 24 hours please call us.";
$signature   = "Management & Staff Fan's Restaurant.";
?>

The first 6, which I use for placeholders in my contact.php (included on the main contact page), are working fine. But the last 3, which I need in in the process_contact_form.php file are not working. I call the processing page true a ajax call:


            $.ajax({
                  type: "POST",
                  url: "../modules/site/process_contact_form.php",
                  data: dataString,
                  cache: false,

What could be the reason that those last three are not working. Could it be be because process_contact_form.php is included in a file contact.php, which is by it self included as well. Should I store them as global variables and if so how should I do that?

Thank you in advance

What do you mean by “not working”?
The PHP code you posted is just a few variables with their values.
If you call that through AJAX, nothing will be returned.

Hi Guido. Thank you for your responce. So how do I pass those variable (stored in translations_en.php) to process_contact_form.php which is indeed called true AJAX?

Do an include of translations_en.php in process_contact_form.php ?

Hi Guido. I was thinking of that. The problem I have is that all files, except for the default ones (dutch) are located in their own directory (en/fanskitchen-contact.php, de/fanskitchen-contact.php, it/fanskitchen-contact.php, etc). Sow when I am on the Italian contact page: it/fanskitchen-contact.php. translations_it.php should be included and when I am on the English one it/fanskitchen-contact.php, translations_en.php should be included etc. How should I approach that.

You’ll have to send the current language to the PHP script through the ajax request, and then use that in the script to include the correct language file.

Hi Guido. And how should I send the current language? Or should I continue this question in the Javascript forum?

If you don’t know how to send variables through a jquery ajax call, then yes, you should ask that question in the JS forum.
But since this thread hasn’t really solved anything yet, I’m just going to move it there :wink:

Hi Guido.Thank youfor relocating this threat to the Javascript forum.

The situation:I pass certain form values with a AJAX call the following way:


var dataString = '$name='+ name + '&email='+ email + '&phone='+ phone + '&subject='+ subject + '&comment='+ comment;
$.ajax({
  type: "POST",
  url: "../modules/site/process_contact_form.php",
  data: dataString,
  cache: false,
  beforeSend: function(){
    $("#send_status").html('<div style="padding-left:100px;"><img src="loading.gif" alt="Sending...." align="absmiddle" title="Sending...."/> <font style="font-family:Verdana, Geneva, sans-serif; font-size:11px; color:black;">Please wait...</font></div><br clear="all">');
    },
    success: function(response){
      $("#send_status").html($(response).fadeIn(2000));
      $(".contL").fadeOut(1000);
      $("#name").val('');
      $("#email").val('');
      $("#phone").val('');
      $("#subject").val('');
      $("#comment").val('');
						
    }
});

Next to the form values i need to send the following 3 PHP variables to process_contact_form.php


$salutation  = "Dear";
$message   = "
Your message has been successfully sent and we will contact you as soon as possible If you have not received a response from us within 24 hours please call us.";
$signature   = "Management & Staff Fan's Restaurant.";
?>

How do I add those 3 variables to the above AJAX call.

Thank you in advance!!!

Hi donboe,

You can’t.

PHP is interpreted on the server and the resultant HTML is sent to the browser.
It is only the HTML that you can manipulate.
JS has no concept of PHP variables.

Ok, what I think you should do is:

AJAX call:


var dataString = '$name='+ name + '&email='+ email + '&phone='+ phone + '&subject='+ subject + '&comment='+ comment[COLOR="#FF0000"][B] + '&language='+ language[/B][/COLOR];

Where the JS variable ‘language’ would contain the current language.

Then in the process_contact_form.php, you include the correct language file using the selected language:


// sanitize and validate the variables sent by the ajax call
$language = whateveryouneedtodotosanitizeandvalidate($_POST['language']);
$languagefile = 'translations_' . $language . '.php';
include $languagefile;

Hi Guido. I think I understand the principle what you want to accomplish. But after I added + ‘&lang=’+ lang (langis the variable I use to determine the language) to var datastring the form wasn’t proccessed any longer. By the way this is the entire Javascrip function:


function submit_contact_form(){
      var reg = /^([A-Za-z0-9_\\-\\.])+\\@([A-Za-z0-9_\\-\\.])+\\.([A-Za-z]{2,4})$/;
      var name = $("#name").val();
      var email = $("#email").val();
      var phone = $("#phone").val();
      var subject = $("#subject").val();
      var comment = $("#comment").val();

      if(name == "")
      {
            $("#send_status").html('<div class="error" style="color: #C00">Vul uw naam in om verder te gaan.</div>');
            $("#name").focus();
      }
      else if(email == "")
      {
            $("#send_status").html('<div class="error" style="color: #C00">Vul uw e-mailadres in om verder te gaan.</div>');
            $("#email").focus();
      }
      else if(reg.test(email) == false)
      {
            $("#send_status").html('<div class="error" style="color: #C00">Vul een geldig e-mailadres in om verder te gaan.</div>');
            $("#updates_subscription").focus();
      }
      else
      {
            var dataString = 'name='+ name + '&email='+ email + '&phone='+ phone + '&subject='+ subject + '&comment='+ comment + '&lang='+ lang;
            $.ajax({
                  type: "POST",
                  url: "../modules/site/process_contact_form.php",
                  data: dataString,
                  cache: false,
                  beforeSend: function()
                  {
                       $("#send_status").html('<div style="padding-left:100px;"><img src="loading.gif" alt="Sending...." align="absmiddle" title="Sending...."/> <font style="font-family:Verdana, Geneva, sans-serif; font-size:11px; color:black;">Please wait...</font></div><br clear="all">');
                  },
                  success: function(response)
                  {
                        $("#send_status").html($(response).fadeIn(2000));
			$(".contL").fadeOut(1000);
                        $("#name").val('');
                        $("#email").val('');
			$("#phone").val('');
			$("#subject").val('');
			$("#comment").val('');
                  }
            });
      }
}

Should I maybe add the &lang to the success: function ?

Thank you in adance.

Hi Guido. I finally got it working. I took a slightly different approach. I just added 3 hidden fields to the contact form with the value of those 3 variables that were not working:


		  <input type="hidden" id="salutation" value="<?php echo $salutation;?>" />
		  <input type="hidden" id="message" value="<?php echo $message;?>" />
		  <input type="hidden" id="signature" value="<?php echo $signature;?>" />

That way they are picking up the right value depending on the includd translation file. I added those to the datastring and it is working like a charm. Thank you for all your patience and input :tup: