#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Mar 2001
    Location
    England
    Posts
    673
    Rep Power
    14

    PHP And JavaScript form problem


    This is my code:

    PHP Code:
    <?php

    $db 
    mysql_connect("localhost""root");

    mysql_select_db("pyramid",$db);

    $result mysql_query("SELECT * FROM house where town='$town'",$db);

    echo 
    "<form name=\"formName\" action=\"vacancies4.php\" method=post>";

    while (
    $myrow mysql_fetch_row($result)) {

    echo 
    "<tr><td width=\"379\" colspan=\"2\" align=\"left\" valign=\"top\" nowrap class=\"ourpubs\">

    <table><tr><td><img src=\"images/trans.gif\"  width=\"1\" height=\"8\" border=\"0\"></td></tr>

    <tr><td><input type=\"hidden\" name=\"jio\" value=\""
    $myrow[0], "\"><a href=\"#\" onclick=\"document.formName.submit();return false\" style=\"text-decoration:underline; color:black\"><b><font class=\"be\">"$myrow[1], "</font></b></a></td></tr>

    <tr><td>"
    ,  $myrow[2], ", ",  $myrow[3], ", ",  $myrow[4], ", ",  $myrow[5], "</td></tr>

    <tr><td>&nbsp;</td></tr><tr><td>"
    ,  $myrow[6], "</td>
    </tr></table></td></tr>

    <tr><td><img src=\"images/trans.gif\"  width=\"1\" height=\"8\" border=\"0\"></td></tr
    }
    echo "
    </form>";
    ?>
    What is does is display all the records from the database on the web page and you can click on $myrow[1] and be taken to the next page. I am using $myrow[1] as the submit button in a form as you can see by the line

    <a href=\"#\" onclick=\"document.formName.submit();return false\" style=\"text-decoration:underline; color:black\"><b><font class=\"be\">", $myrow[1], "</font></b></a>

    The <form> tag is before the search and the </form> is after.

    I need to pass the hidden field through to the next page hence the line

    <input type=\"hidden\" name=\"jio\" value=\"", $myrow[0], "\"> is put next to the echo $myrow[1], but when the page is parsed, each record has one of these hidden fields and hence clicking on the submit button passes through whichever is the value in the last hidden field, not the one next to the record.

    Is there any way around this?

    Many thanks

    James
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2001
    Location
    Toronto, Ontario, Canada
    Posts
    631
    Rep Power
    14
    PHP Code:
    <?php
    $db 
    mysql_connect("localhost""root");
    mysql_select_db("pyramid",$db);
    $result mysql_query("SELECT * FROM house where town='$town'",$db);
    echo 
    "<form name=\"formName\" action=\"vacancies4.php\" method=post>";
    $i 0;
    while (
    $myrow mysql_fetch_row($result)) {
        echo 
    "<tr><td width=\"379\" colspan=\"2\" align=\"left\" valign=\"top\" nowrap class=\"ourpubs\">

        <table><tr><td><img src=\"images/trans.gif\"  width=\"1\" height=\"8\" border=\"0\"></td></tr>

        <tr><td><input type=\"hidden\" name=\"jio[
    $i]\" value=\""$myrow[0], "\"><a href=\"page?access={$i}\" onclick=\"document.formName.submit();return false\" style=\"text-decoration:underline; color:black\"><b><font class=\"be\">"$myrow[1], "</font></b></a></td></tr>

        <tr><td>"
    ,  $myrow[2], ", ",  $myrow[3], ", ",  $myrow[4], ", ",  $myrow[5], "</td></tr>

        <tr><td>&nbsp;</td></tr><tr><td>"
    ,  $myrow[6], "</td>
        </tr></table></td></tr>

        <tr><td><img src=\"images/trans.gif\"  width=\"1\" height=\"8\" border=\"0\"></td></tr>"
    ;
        
    $i++;
    }
    echo 
    "</form>";
    ?>

    Then to access that variable on the next page:
    $jio[$access]
  4. #3
  5. Newbie :P
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Jan 2001
    Location
    In the PHP Engine :-)
    Posts
    2,880
    Rep Power
    21
    Hi James,

    Your problem is because the name of the hidden field is always the same, if you were to run that code and view source, stripping out all your other code you would end up with something like this:

    PHP Code:
    <input type="hidden" name="jio" value="1"
    <
    input type="hidden" name="jio" value="2"
    <
    input type="hidden" name="jio" value="3"
    Depending on the data that $myrow[0] actually contains and the number of records returned. So the next page is always going to take the last value of 'jio' in the above example this would be 3.

    What you need to do is use arrays, by changing this part of your code:

    PHP Code:
    <input type=\"hidden\" name=\"jio\" value=\""$myrow[0], "\"> 
    to this:

    PHP Code:
    <input type=\"hidden\" name=\"jio[]\" value=\""$myrow[0], "\"> 
    Note the [] after jio

    We pass the data in an array, so when $jio gets to the next page it is an array which contains (based on my above example)

    0 => 1
    1 => 2
    2 => 3

    Where 0,1,2 are the keys and 1,2,3 are the elements of data stored in the array. So now all you need to do is figure out which element of the data you need in the new page.

    HTH
    ---------------------
    -- SilkySmooth --
    ---------------------
    Proxy
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Mar 2001
    Location
    England
    Posts
    673
    Rep Power
    14
    Thanks for your help but this doesn't wok at all I'm afraid. Nothing is passed through - when the page is parsed the code

    <input type=\"hidden\" name=\"jio[$i]\" value=\"", $myrow[0], "\"><a href=\"page?access={$i}\" onclick=\"document.formName.submit();return false\" style=\"text-decoration:underline; color:black\">

    turns into:

    <input type="hidden" name="jio[]" value="1"><a href="page?access=" onclick="document.formName.submit();return false" style="text-decoration:underline; color:black"><b><font class="bealandlord">The Rose & Crown</font></b></a

    Any ideas?.

    Thanks,

    James
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Mar 2001
    Location
    England
    Posts
    673
    Rep Power
    14
    OK, thanks Silky - I'll try and learn about arrays.

    Cheers,

    James
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Mar 2001
    Location
    England
    Posts
    673
    Rep Power
    14
    Well, I can't get the hang of these arrays. All I get is either nothing or the word ARRAY on the next page.

    What am I doing wrong guys?

    Just to see it work, I'm using echo $jio[]; on the second page

    Any help appreciated.

    James
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Mar 2001
    Location
    England
    Posts
    673
    Rep Power
    14
    If I put $jio[0] - that works, but doesn't give the result I want - I presume I have to increment the value dynamically somehow?

    James
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2001
    Location
    Toronto, Ontario, Canada
    Posts
    631
    Rep Power
    14
    The value of $i not being replaced is probably because you didn't copy the whole thing. But really, you don't need to use that. You can use $array[]. However, this is ONLY for setting the value of an array, not for reading.

    On the next page, to access the value you want, $access, defined in the address, contains the key you want to access the right value of the array.
    $jio[$access]

    This works because $access = $i, and the key of the array = $i. This is defined by page.php?access=$i in you <a> you use to submit the form. Each <a> has a different value for $i / $access which corresponds to each $jio that is defined.

    You can also do this,

    PHP Code:
    <?php

    $db 
    mysql_connect("localhost""root");

    mysql_select_db("pyramid",$db);

    $result mysql_query("SELECT * FROM house where town='$town'",$db);
    $i 0;
    while (
    $myrow mysql_fetch_row($result)) {

    echo 
    "<form name=\"formName$i\" action=\"vacancies4.php\" method=post><tr><td width=\"379\" colspan=\"2\" align=\"left\" valign=\"top\" nowrap class=\"ourpubs\">

    <table><tr><td><img src=\"images/trans.gif\"  width=\"1\" height=\"8\" border=\"0\"></td></tr>

    <tr><td><input type=\"hidden\" name=\"jio\" value=\""
    $myrow[0], "\"><a href=\"#\" onclick=\"document.formName.submit();return false\" style=\"text-decoration:underline; color:black\"><b><font class=\"be\">"$myrow[1], "</font></b></a></td></tr>

    <tr><td>"
    ,  $myrow[2], ", ",  $myrow[3], ", ",  $myrow[4], ", ",  $myrow[5], "</td></tr>

    <tr><td>&nbsp;</td></tr><tr><td>"
    ,  $myrow[6], "</td>
    </tr></table></td></tr>

    <tr><td><img src=\"images/trans.gif\"  width=\"1\" height=\"8\" border=\"0\"></td></tr>
    </form>"
    ;
    $i++;
    }

    ?>

    Where each value is in a separate form.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Mar 2001
    Location
    England
    Posts
    673
    Rep Power
    14
    Hello,

    I tried incrementing the form name as you suggested but this just gives a javascript error:

    error

    document.formName is null or not an object.

    The problem seems to be the line

    $i = 0;

    If you remove this line it appears to work and the form name is incremented but the same old problem applies. Even though they are in different forms now, all records on a page push through the same value.

    Any ideas?

    Cheers

    James
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2001
    Location
    Toronto, Ontario, Canada
    Posts
    631
    Rep Power
    14
    <a href=\"#\" onclick=\"document.formName.submit();return false\" style=\"text-decoration:underline; color:black\">

    Change that to either 1:
    <a href=\"#\" onclick=\"this.form.submit();return false\" style=\"text-decoration:underline; color:black\">
    I don't know if that will work

    or 2:
    <a href=\"#\" onclick=\"document.formName{$i}.submit();return false\" style=\"text-decoration:underline; color:black\">
    for this one, you need to increment the form name.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2001
    Location
    Toronto, Ontario, Canada
    Posts
    631
    Rep Power
    14
    If those don't work you'll have to go back to arrays.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Mar 2001
    Location
    England
    Posts
    673
    Rep Power
    14
    Thanks a lot - the second one seems to work - fingers crossed!!

    James

IMN logo majestic logo threadwatch logo seochat tools logo