gandalf1959
gandalf1959

Reputation: 9

send a php variable from a php "while" loop to a javascript function: not working

here is the general scenario: I need to insert a number of lessons details where some data are the same (title of the lesson, price, etc...) and other are specific for each date (year, month and day, details of the single class...) So I wrote a first page where the user inserts the common data of the lessons cycle, then submits and is sent to a second page where the number of appointments can be specified. With that number I wrote a While loop which generates as many lines with a form as needed. In the same time I generate a idform variable to identify all the forms with a different name. In the submit button I want to send the $idform to a javascript function (will be an Ajax script). Usually you will write:

<input type="button" value=" REGISTRA " style="display:block" onClick="javascript:formami('$idform');">

using the double quotes for containers and single quotes for content. But the point is that I am inside a while loop, and I generate the td with echo, like this:

echo "<input type='button' value=' REGISTRA ' style='display:block' onClick='javascript:formami($idform);'>";

So I cannot use the single quotes for $idform. If I use the single quotes, the variable is not passing at all, if I don't use the quotes instead of the variable I have a [object HTMLFormElement] message. BTW now the function in the head of the document contains only an alert for test purposes:

function formami(numeroriga) {
   var pippo = numeroriga;
   alert("Ciao "+pippo);
}

The result I obtain when clicking on the submit button is an alert with "Ciao [object HTMLFormElement]", when I was expecting the message "Ciao riga1", "Ciao riga2" and so on, depending on which button I was clicking.

Any idea about a possible solution to this problem? may be I have to change the whole logìc behind?

Thanks in advance for all the suggestions

Upvotes: 0

Views: 94

Answers (4)

321zeno
321zeno

Reputation: 1274

You should try using the alternative syntax for control structures, it will make escaping easier and your code will be more readable:

<table>
    <?php foreach ($idforms as $idform): ?>
        <tr>
            <td><?php echo $idform ?></td>
            <td><input type="button" value="REGISTRA" onClick="javascript:formami('<?php echo $idform ?>');"></td>
        </tr>
    <?php endforeach ?>
</table>

Upvotes: 1

You could use this:

<input type="button" value="REGISTRA" style="display:block" onClick="javascript:formami('<?php echo $idform;?>');">

And after you can check source code to check if "formami('idform should be printed here') is getting right value from php echo

Upvotes: 1

Risul Islam
Risul Islam

Reputation: 806

Try

echo "<input type='button' value=' REGISTRA ' style='display:block' onClick='javascript:formami('".$idform."');'>";

Upvotes: 0

Adrianopolis
Adrianopolis

Reputation: 1292

You can use the double quotes and single quotes as long as you escape them in the string such as:

echo "<input type=\"button\" value=\" REGISTRA \" style=\"display:block\" onClick=\"javascript:formami('$idform');\">";

Upvotes: 1

Related Questions