1

I will be very grateful if you could help me.

The data add to the DB only when each data-string have a unique id. It's necessary to assign the id to each string with the click on the button.

How to transform the code that variable $i increase with each click, but there is only the one string in the table at the beginning?

 <?
    for($i=1; $i<2; $i++):
    ?>

                <tr>
                    <td>
                        <label>
                            <input type="text" name="<?=$i?>[file]" value="<? $p=$_POST[$i]; echo($p['file'])?>" >
                        </label>
                    </td>
                    <td>
                        <label>
                            <input type="integer" name="<?=$i?>[xx]" value="<? $p=$_POST[$i]; echo($p['xx'])?>" >

                        </label>
                    </td>
                    <td>
                        <label>
                            <input type="integer" name="<?=$i?>[yy]" value="<? $p=$_POST[$i]; echo($p['yy'])?>">
                        </label>
                    </td>
                    <td>
                        <button type="button" id="add">+</button>
                        <button type="button" id="del">-</button>
                    </td>
                </tr>
        <script>

            $(document).ready(function(){
                $("#add").click(function(){
                <?$i=$i+1;?>
                $('#dynamic').append('<tr><td><label><input type="text" name="<?=$i?>[file]" value=""></label></td><td><label><input type="integer" name="<?=$i?>[xx]" value=""></label></td><td><label><input type="integer" name="<?=$i?>[yy]" value=""></label></td><td><button type="button" class="add">+</button> <button type="button" class="del">-</button></td></tr>');

                });
            });
        </script>

    <?endfor;?>
            </tbody>
        </table>

        <input name="sub" type="submit" value="Create a graph" style="margin: 14px">


    </form>
2
  • What's about using hidden fields? <input type="hidden" name="xyz" value="here_your_i"> Commented Feb 1, 2015 at 16:39
  • 2
    It looks like you're trying to update a php variable which would run on the server side by using javascript which is client side. Is this the case? Commented Feb 1, 2015 at 16:46

2 Answers 2

1

One solution would be to get the value of $i from your database on every click and increment it. Another solution is to store the value of $i in a session and get that value on every click.

Sign up to request clarification or add additional context in comments.

Comments

1

As stated in the comments, it looks like you're trying to run server-side code within a client-side function. This will not work as desired - the line of PHP will execute before the markup is sent back to the browser and rendered, and nothing will happen to this variable when a button is clicked.

My approach would be to make an ajax call to a page with an action parameter passed in (vote.php?action=add or vote.php?action=del, for example). You then increment or decrement this in the DB.

Now if you wish to display the new value on this page (for example between the two buttons, as with StackOverflow), you should use $(selector).load("vote.php?action=add|del") and then ensure that this page only outputs the new result.

Comments

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.