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/>";
}
?>