0

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/>";
  }
?>
1
  • Please describe in more detail what the problem is. Also, specify any PHP errors and JavaScript errors you get. That is: enable error reporting in PHP, and inspect the console tab of the developer tools in your browser. Commented Nov 28, 2014 at 21:50

1 Answer 1

1

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.. */
?>
Sign up to request clarification or add additional context in comments.

6 Comments

Sorry, I made a mistake. The variable is "name", not "nome". I think I will not change that part since it works ok (and I have to do in this way because the project is much more complex. I have just posted an example). The problem is the second part. I need to get the "name" variable and pass it to php (without refreshing the page). Then, it would be put into the array's value that was empty before.
I have updated my answer to give you a sense of how to solve it. I can't guarantee it will work to your needs. You may have to modify (especially the query parameters) to suit your problem.
Are you sure $string = "<br /> It has been a long time since [[red]].." actually replaces [[red]] with the value of $array["[[red]]"]?
No, it does not. I have another part of code for that purpose. It works. I have not posted it here because it makes no problem. But before executing that part I need to fill the empty values into the array. I will try the code you have posted and I will let you know. Thanks!
No, it does not work. I have the same problem. I am not able to pass the variable I get from the pop up to PHP. This piece of code, it does not work: var xmlhttp=new XMLHttpRequest(); xmlhttp.open("GET","index.php?name=" + name + "&key=" + <?php echo $key?>,true); xmlhttp.send(); When I try to get the varibales with $_REQUEST, there is nothing inside them.
|

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.