Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Dec 2002
    Posts
    31
    Thanks
    1
    Thanked 0 Times in 0 Posts

    building form names & values with php

    Hello,

    I have created a mysql table that contains the following fields
    id
    recipe_id
    quanity
    ingredient

    I have sucessfully written a php script where upto 30 records can be populated into this table.

    What I am struggling with is I need to allow the user to edit the recipe record

    the input fields are <input type=text name=QTY[1]><input type=text name=ING[1]>

    Where this could be anything between 1 and 30 sets of these tags depending on how many pairs the user entered data into.

    I am guessing that I need to get the records from the table as follows...

    and I only need to display the pairs of fields where the quantity field is NOT blank.

    $viewmemberssql3 = "SELECT * FROM recipe_item WHERE recipe_id='$id' order by id asc";
    $viewmembersquery3 = @mysql_query($viewmemberssql3,$connection) or die(mysql_error());
    while ($viewmembersrow3 = mysql_fetch_array($viewmembersquery3))

    {

    if ($viewmembersrow3['quantity']!="")

    {


    print $viewmembersrow3['quantity']; print "<br>"; print $viewmembersrow3['ingredient']; print "<br>"; print $viewmembersrow3['id']; print "<br>";
    }

    }

    This code seems to be OK but I am struggling to automatically build the tags for the form so that the user can edit them?

    I am sorry if I haven't made myself very clear???????

  • #2
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    In the function, put the variables to edit. Such as

    PHP Code:
    function edit($id) { 
    Rowsdower! has accused me of having mental problems, and the administrator allowed it. What a great forum huh?

  • #3
    New Coder
    Join Date
    Dec 2002
    Posts
    31
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hello,

    Thanks I seem to have it cracked now....

    Could I please ask you this question.

    Whilst pulling the records out of the table I increment var x in php by saying x++;

    I could then do with using that counter in my javascript function that them immediatly follows with a for loop as follows...

    <script type="text/javascript">
    for ( var i = 2; i <= 30; ++i )
    {
    document.write( '<tr id="L' + i + '">'
    + '<td><input name="QTY[' + i + ']" onchange="showNextLine(this);" /></td>'
    + '<td><input name="ING[' + i + ']" onchange="showNextLine(this);" /></td>'


    + '</tr>');
    }
    </script>

    What I need to do is to replace the highlighted red number to with the x php variable but when I simply replace the 2 with x I get a javascript error to say that x is not defined?

    I suppose my question is how to I pass a variable from php to javascript?

    I hope you can help.

    Regards


    Rowan

  • #4
    Senior Coder ahallicks's Avatar
    Join Date
    May 2006
    Location
    Lancaster, UK
    Posts
    1,134
    Thanks
    1
    Thanked 57 Times in 55 Posts
    You can't, basically. What you can do is store the value of x somewhere on your page, such as in a hidden input:

    Code:
    <input type="hidden" name="x-val" id="x-val" value="<?php echo $x; ?>" />
    Then in javascript:

    Code:
    var i = document.getElementById('x-val').value;
    "write it for FireFox then hack it for IE."
    Quote Originally Posted by Mhtml View Post
    Domains are like women - all the good ones are taken unless you want one from some foreign country.
    Reputation is your friend

    Development & SEO Tools

  • #5
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts
    Can't you just do:

    Code:
    for ( var i = <?php echo $x; ?>; i <= 30; ++i )
    ?

  • #6
    New Coder
    Join Date
    Dec 2002
    Posts
    31
    Thanks
    1
    Thanked 0 Times in 0 Posts
    great idea spudhead, I will give that a go!

  • #7
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    You could also just echo out that javascript loop using php instead. Theres no reason why php can't do that for you
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •