How can send the value of a variable from page to another page

Discussion in 'PHP' started by lionking, May 21, 2009.

  1. #1
    Hello everybody

    At first i have 2 php page

    The first page get the (id,name, sorting) of the sections from database
    The second page Receiving the data (id,name, sorting) to editing or deleting from database

    in the first page every section has id number to distinguish between each section and the other

    I want to do is send section ids and other data to second page by Ajax
    and I am already do it so successfully but i have One problem

    Receive all the sections are single id

    Example

    section1 -- id(20)
    section2 -- id(15)
    section3 -- id(33)
    section4 -- id(71)

    i'm Receiving all the sections id are id(33)
    i have used session and hidden field but Occurs with the same problem


    The first PHP page code (get the (id,name, sorting) of the sections from database)

    
    <table id='tab' class='sty' border='1' cellpadding='5' cellspacing='5'>
                <tr>
                    <td>
                        Order
                    </td>
                    <td>
                        Section Name
                    </td>
                    <td>
                        Editing
                    </td>
                </tr>
                <?php
                $get = mysql_query("select * from cat");
                while ($row = mysql_fetch_array($get)) {
                    //echo "<form action='page4.php' method='POST'>";
                    echo "<tr><td>";
                    echo "<input type='text' name='sec_sort' id='sec_sort' value='".$row['sorting']."' size='1' />";
                    echo "</td><td>";
                    echo "<input type='text' name='sec_name' id='sec_name' value='".$row['catname']."' />";
                    echo "</td><td>";
                    echo "<input type='button' name='edit_name_button' id='edit_name_button' value='Edit' onclick='edit_section()' />";
                    echo "</td></tr>";
                    echo "<input type='hidden' name='getid' id='getid' value='".$row['catid']."' />";
                    //echo "</form>";
                }
                ?>
            </table>
    
    
    PHP:

    The second PHP page code (Receiving the data (id,name, sorting) to editing or deleting from database)

    
    
    <?php
            $sec_sort = $_POST['sec_sort'];
            $sec_name = $_POST['sec_name'];
            $getid = $_POST['getid'];
            
            if ( isset ($_POST['edit_name_button'])) {
                $edit_sec = mysql_query("update cat set catname='$sec_name',sorting='$sec_sort'  where catid='$getid'");
                if ($edit_sec) {
                    echo $getid;
                    echo "the data has been updated";
                }
                else {
                    echo $getid;
                    echo "Error, no data is updated";
                }
            }
    ?>
    
    PHP:


    The ajax code (Which sends the data id,name, sorting to page 2)

    
    
    function edit_section(){
        var sec_sort = document.getElementById('sec_sort').value;
        var sec_name = document.getElementById('sec_name').value;
        var edit_name_button = document.getElementById('edit_name_button').value;
        var getid = document.getElementById('getid').value;
        
        
        Http.onreadystatechange = function(){
            if (Http.readyState == 4) {
                document.getElementById('result').innerHTML = Http.responseText;
            }
            
            else {
                document.getElementById('result').innerHTML = "loading";
            }
        }
        
        
        url = "sec_sort=" + sec_sort + "&sec_name=" + sec_name + "&edit_name_button=" + edit_name_button + "&getid=" + getid;
        Http.open("POST", "page4.php", true);
        Http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        Http.send(url);
    }
    
    
    Code (markup):
    I explained the problem in detail

    I wish help from all
     
    lionking, May 21, 2009 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    As far as I can see, you're not differentiating the names when you get the data in the first page. That means that every part of the first page you try to send has the same names for all the variables, including the ID - which means that it probably gets overwritten, and only one value gets transferred?
     
    PoPSiCLe, May 21, 2009 IP
  3. TecBrat

    TecBrat Member

    Messages:
    31
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    48
    #3
    If you put square brackets [] after your variable names, that should make them arrays. Then your second page could access them as $getid[0],$getid[1]...
     
    TecBrat, May 21, 2009 IP
  4. lionking

    lionking Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thank You MR PoPSiCLe and Mr TecBrat For Help
     
    lionking, May 21, 2009 IP