user3563844
user3563844

Reputation: 113

Passing a variable from AJAX to PHP

I am working on a php project. I have an associative array which I use to make a dictionary. I have also a string with a text where are included some keys of the array. What I want to do is generating another String with the same text of the first one but where key words are replaced with array's values.

Before the sostitution I want the user writes in each empty-value of the array (which compares into the String) the contents. I have to do it throw a pop up. I need to get the variable's content of the Javascript variable and put it into the PHP array. I have tried to use AJAX but I am a biginner and I do not know if I am doing well.

Here my code (which it does not work):

<?php
  $array["[[red]]"] = "Once upon a time";
  $array["[[blue]]"] = "fox";
  $array["[[black]]"] = "cat";
  $array["[[orange]]"] = "";

  $string = "<br /> It has been a long time since [[red]] "
  . "[[blue]]. My name is [[blue]] "
  . "and my surname is [[black]]. <br />"
  . "My age is[[orange]]. <br /> <br />.";

  echo "First string': ". $string;
?>
<br /> <br />

<?php
  foreach ($array as $key => $value)
  {
    if ((strstr($string, $key) == true) && ($value == ""))
    {
?>
      <script>
        name = prompt("Insert a correct value: ");
        while ((name== "") || !(isNaN(name)) || name== null)
        {
          window.alert("Wrong insert!");
          name= prompt("Insert a correct value: ");
        }

        var xmlhttp=new XMLHttpRequest();

        xmlhttp.open("GET","index.php?name="+name,true);
        xmlhttp.send();
      </script>

<?php
        $q = $_REQUEST["name"];
        $value = $q;
        echo "the value is  ".$q;
    }
    //echo $key." => ".$value;
    echo "<br/> <br/>";
  }
?>

Upvotes: 0

Views: 118

Answers (1)

ivan.sim
ivan.sim

Reputation: 9268

To generate a string with key words being replaced by values in your array, you can use the sprintf function like this:

$array["red"] = "Once upon a time";
$array["blue"] = "fox";
$array["black"] = "cat";
$array["orange"] = "";

$sentence = "<br />It has been a long time since %s %s. My name is %s and my surname is %s.<br /> My age is %s. <br /><br />.";
echo "First string" . sprintf($sentence, $array["red"], $array["blue"], $array["blue"], $array["black"], $array["orange"]);

As for the second part of your question, where you want to capture the user's inputs to fill up empty entry in the array... well, you need to first correct your nome js variable with name.

Then you need to submit both the keys and the user-provided value in your AJAX call, so that you know which entry in your associative array to update. It will be easier to capture the query parameters before your $array is instantiated.

Also, strstr doesn't return true even if the needle is present in the haystack. Take a look at the doc.

So maybe something like:

<?php
  // you may want to modify the handling of the query parameters to suit your needs
  if(isset($_REQUEST['key']) && isset($_REQUEST['name'])
    $array[$_REQUEST['key']] = $_REQUEST['name'];  

  $array["red"] = "Once upon a time";
  $array["blue"] = "fox";
  $array["black"] = "cat";
  $array["orange"] = "";

  foreach ($array as $key => $value) {
    if (strstr($string, $key) && $value.empty()) {
?>
    <script>
      name = prompt("Insert a correct value: ");
      while ((name== "") || !(isNaN(name)) || name== null) {
        window.alert("Wrong insert!");
        name= prompt("Insert a correct value: ");
      }

      var xmlhttp=new XMLHttpRequest();
      xmlhttp.open("GET","index.php?name=" + name + "&key=" + <?php echo $key?>,true);
      xmlhttp.send();
    </script>

<?php
    }
  }

  /* echo etc.. */
?>

Upvotes: 1

Related Questions