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. ezprint2008

    ezprint2008 Well-Known Member

    Messages:
    611
    Likes Received:
    15
    Best Answers:
    2
    Trophy Points:
    140
    Digital Goods:
    1
    #2
    why did you post this SO many times to the forum .. lol turd.

    This is a basic PHP question , real basic. Are you just now starting to learn PHP?
    variables are passed in giving them names in HTML elements, like forms.
    then use that name to pick it up in PHP with $_POST['name_of_your_element'];
    and make it into a variable
    $nameofelement = $_POST['name_of_element'];

    come on dude, you aint even tryin. :D
     
    ezprint2008, May 21, 2009 IP
  3. lionking

    lionking Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    for post this SO many this is error in form not from me

    and Please look at my post well to know what i Want

    thank you
     
    lionking, May 21, 2009 IP
  4. haradeep

    haradeep Well-Known Member

    Messages:
    231
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #4
    U can include the first php file in the second php file.
     
    haradeep, May 21, 2009 IP
  5. ezprint2008

    ezprint2008 Well-Known Member

    Messages:
    611
    Likes Received:
    15
    Best Answers:
    2
    Trophy Points:
    140
    Digital Goods:
    1
    #5
    it sounds like youre tyring to call multiple array variables of seperate 'sections' , but youre calling it and trying to turn it into one variable...and what PHP will do is go through and take the very last one as the variable.

    //example.
    if you have a list:
    blue
    red
    green
    yellow

    and call that list, PHP will say your variables are "yellow"
    because each line it resets the variable value..ending on the last one.
     
    ezprint2008, May 21, 2009 IP
  6. haradeep

    haradeep Well-Known Member

    Messages:
    231
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #6
    U can pass value by using URL(Use get method)

    Example
    index.php?value=(here print the variable)

    In the next php file
    $id=$_GET(value)
     
    haradeep, May 21, 2009 IP
  7. haradeep

    haradeep Well-Known Member

    Messages:
    231
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #7
    U have to pass the variables by using post or get or sessions
     
    haradeep, May 21, 2009 IP
  8. lionking

    lionking Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    thank you very much mr ezprint2008 and mr haradeep for Help
     
    lionking, May 21, 2009 IP
  9. websea

    websea Peon

    Messages:
    572
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    You can use global variable or session variable to store the value. Add session_start() on the top of the page where you want to have the session variable value.
     
    websea, May 22, 2009 IP