-3

I would like to use the result of a JavaScript function for the parameter of a PHP function.

This will generate a random ID:

  function generate_id()
    {
        var text = "";
        var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

        for( var i=0; i < 5; i++ )
            text += possible.charAt(Math.floor(Math.random() * possible.length));

        return text;
    }

The 5th parameter of the function bellow should contain the result of the JS function.

$anrede_frau = $html_helper->form_input("radio", "friend_anrede[]", array("anrede" => array("Frau", "Madame", "Signora")), "input_field_left", 'anrede_<script>generate_id()</script>', 'left');
$anrede_herr = $html_helper->form_input("radio", "friend_anrede[]", array("anrede" => array("Herr", "Monsieur", "Signor")), "input_field_left", 'anrede_<script>generate_id()</script>');

I tried to use something like anrede_<script>generate_id()</script> but that doesn't work. How can I get this fixed?

EDIT: The idea is to generate a new input field with a random ID. this should happen whenever a button with the ID #addMore is clicked. This is the way I am doing it:

<script>
$(function() {
  $("#addMore").click(function(e) {
         e.preventDefault();
         $(".form_area").append('<br><br><?= $html_helper->form_input("radio", "friend_anrede[]", array("anrede" => array("Frau", "Madame", "Signora")), "input_field_left", 'anrede_frau_ + generate_id() + '); ?>');
         $(".form_area").append('<?= $html_helper->form_input("radio", "friend_anrede[]", array("anrede" => array("Herr", "Monsieur", "Signor")), "input_field_left", 'anrede_herr_ + generate_id() + '); ?>');
         $(".form_area").append('<?= $html_helper->form_input("text", "friend_vorname[]", array("vorname" => array("Vorname", "Prénom", "Nome")), "input_field_left"); ?>');
         $(".form_area").append('<?= $html_helper->form_input("text", "friend_name[]", array("name" => array("Name", "Nom", "Cognome")), "input_field_right"); ?>');
         $(".form_area").append('<?= $html_helper->form_input("text", "friend_strasse_nr[]", array("strasse" => array("Strasse / Nr.", "Rue/n°", "Indirizzo"))); ?>');
         $(".form_area").append('<?= $html_helper->form_input("text", "friend_plz_ort[]", array("ort" => array("PLZ / Ort", "NPA / localité", "NPA / Località"))); ?>');
         $(".form_area").append('<p class="de">Bitte vollständig ausfüllen.</p><p class="fr">Veuillez remplir complètement.</p><p class="it">Si prega di compilare integralmente.</p>');
      });
    });
</script>

Either with PHP or with JavaScript I need to be able to add an ID to my input fields with the type of radio. I hope that makes my question a bit clearer.

Pom Canys
  • 369
  • 2
  • 5
  • 16
  • AJAX, it's the only way, PHP is executed before JS is. – Epodax Jan 04 '16 at 09:06
  • PHP runs on the server and creates the markup, it then sends that to the users computer, where javascript is running directly in the browser. In other words, by the time javascript runs, it's way to late to do anything with PHP, you'd have to send the data back to the server with ajax, but that still won't update the variable that is already used – adeneo Jan 04 '16 at 09:06
  • 1
    You aren't really doing anything in that Javascript function that you can't do in a PHP function, so just write it in PHP and use it. – jeoj Jan 04 '16 at 09:15

1 Answers1

0

The big thing you need to remember when coding is that PHP is compiled on your server BEFORE your Javascript code is run. Those are two seperate worlds and can't communicate with each other except via AJAX.

Since your function isn't really hard to write in PHP, you can create a PHP function that does the same as your Javascript function and fetch this result with AJAX.

Nerdben
  • 68
  • 5
  • 1
    ...except via **HTTP requests**. A regular link or form submission would also constitute "communication" between Javascript and PHP; AJAX just relegates this to a background task. – deceze Jan 04 '16 at 09:40
  • @deceze absolutely right. I was just thinking about the cleanest way to fetch a function return without page refreshes etc. – Nerdben Jan 04 '16 at 09:46