声明一个JavaScript变量,它将保存php变量

I have an app for making questionnaires. Users have index.php page where they create the questions and choose minimum number of answers, then they have process.php page where they can enter their answers or add more answers.

PROBLEM: When user clicks add more button, it creates textarea of the particular question but with the wrong name. The add more button should add a textarea and change its name according to the minimum of the defined textareas. So if you for ex. have 4 defined textareas in question2, the next textareas should be like odg25, odg26, odg27, odg28 etc...

The problem is in variable $k (process.php) - because it is not defined in addmore function, but I don't know how to pass somehow in this part of code to make it happen.

THIS IS THE TESTING LINK

INDEX.PHP

<input id="btntxt" type="submit" value="TEXT" onclick="addtxt();" /><br/><br/>

<form action="process.php" method="post">
Title: <br/><input type="text" name="naslov" size="64" required ><br/>
Maximum characters: <br/><input type="text" name="chars" size="64"><br/><br/>
<div id="brain1"></div><br/>
<input type="submit" name="submit" value="CONFIRM"><br/>
</form>

PROCESS.PHP

<script type="text/javascript">
<?php $chars = $_POST['chars']; ?>
function addmore(index) {
    var textarea = document.createElement("textarea");
        textarea.name = "odg" + index + //WHAT SHOULD I ADD HERE???;
        textarea.rows = 3;
        textarea.setAttribute('maxlength',<?php echo $chars ?>);
        var div = document.createElement("div");
        div.innerHTML = textarea.outerHTML;
        document.getElementById("inner"+index).appendChild(div);
}
</script> 
<body>
<?php
$bla = "";
$pitanje = $_POST['question'];
$length = count($_POST['question']);
$req = $_POST['req'];
$requiem = '';
$min = $_POST['min'];
$area = array("","","","","","","","","","","","","","","");

for($j=1; $j<$length+1; $j++) {
if($_POST['question'][$j] != "") {
    if(($min[$j])!="") {
    for($k=1;$k<=$min[$j];$k++) {
    $area[$j] .= '<textarea name="odg'.$j.$k.'" rows="3"'.$requiem.' maxlength="'.$chars.'" ></textarea><br/>';}}
    if(($min[$j])=="") {
    $area[$j] = '<textarea name="odg'.$j.$k.'" rows="3"'.$requiem.' maxlength="'.$chars.'" ></textarea>';}
    $addmore = '<input type="button" name="more" value="Add more" onClick="addmore('.$j.');">';
    $bla .= $j.') '.$pitanje[$j].'<br/>'.$area[$j].'<div id="inner'.$j.'"></div>'.$addmore.'<br/>';}}

echo $bla;
?>

FNCS.JS

var n = 1;
function addtxt() {
        var textarea = document.createElement("textarea");
        textarea.name = "question[" + n + "]";
        var required = document.createElement("input");
        required.type = "checkbox";
        required.name = "req[" + n + "]";
        var minimum = document.createElement("input");
        minimum.type = "text";
        minimum.name = "min[" + n + "]";
        var div = document.createElement("div");
        div.innerHTML = n + ". Question: " + "<br />" + textarea.outerHTML + "<br />" + "Required: " + required.outerHTML + "<br />" + "Min: " + minimum.outerHTML + "<br /><hr/><br/>";
        document.getElementById("brain1").appendChild(div);
        n++;
}

I did the same kind of dev.

I had a globalized counter (cpt) in the javascript is incremented by 1 each duplication My variables were duplicated like this id = "foo_" + cpt.

I added a hidden field for the counter <input type="hidden" id = "cpt"> and its value was changed for each replication.

Php side, I recovered the counter and then a loop to iterate through all the duplicate fields.

// For example
$cpt = $_POST['cpt'];
for ($i = 1; $i <= $cpt; $i++) {
    $foo[$i] = $_POST['foo_' . $i];
}

I hope it will help.

You're mixing JavaScript and PHP. PHP is doing some part of the question generation and then JavaScript has to pick up where it left off.

The problem with that approach is that you'll find you end up duplicating a lot of functionality.

The answer the quesiton WHAT SHOULD I ADD HERE??? is "odg" + $j + $k

If instead you start by doing:

var questions = <?php echo json_encode($_POST["question"]);?>;

You now have all your question data available in JavaScript. You can move the for loop from PHP to JavaScript and have j and k there.

What you're going to have to do is make $k able to be passed into process.php.
That is accomplished with something like this:

<form action="process.php" method="post">
Title: <br/><input type="text" name="naslov" size="64" required ><br/>
Maximum characters: <br/><input type="text" name="chars" size="64"><br/><br/>
<div id="brain1"></div><br/>
<input id="numRows" type="hidden" name="numRows" value="1"/>
<input type="submit" name="submit" value="CONFIRM"><br/>
</form>

notice I've added a new <input> element with the name "numRows" which will be passed via POST to process.php. I've given it an arbitrary default value of 1, you can set this however you wish.

Now, when a user clicks the "add more" button, within fncs.js do this: document.getElementById("numRows").value++;

and finally, in your process.php you need to read in the value of this, as $k:
<?php $k = isset($_POST['numRows']) ? urldecode($_POST['numRows']) : 1; ?>

within process.php you may do as you wish, then, with that value $k.

You need to store last text area value in hidden variable and always increment that

  • first step: At start set value of hidden variable and your counter 'n' same

  • second step : at each step where you are adding new text area ,
    overwrite the hidden value by new counter value of text area

Remember Textarea counter should be always fetched from hidden value

I think this may help you to solve your problem