Skip to content
Advertisement

How to pass image value from variable in statement to another PHP file

Hey so i`m trying to pass value from my PHP file where i pull the data from the database to another PHP file to an tag i have tried to display my images on the same file where i do my query and it does work but i need it in another file. thanks

    if($image!="")
    {
        $res = mysqli_query($link,"SELECT * FROM lug_num WHERE lug_id= $image");
        $array = array();
        ?><br><br><div id = img_lug><?php
        while($row=mysqli_fetch_array($res))
        {
            
            global $img;
            
            $img = $row["image"];
            //echo $img;
            
            ?><img src="<?php echo $img;  ?>"  width='200' height='200' /><?php
            
                
        }
            ?></div><?php

This is my file where i fetch and can display images and i need the $img variable in another file

     <?php
            include ("ajax.php");
            echo $img;
             
            ?><img src="<?php include("ajax.php"); echo $img;  ?>"  width='200' height='200'  /><?php
            ?>

this is me trying to include the variable in the other file but no success. before you say something about the include php file i know it`s twice i was desperate and was trying different things.
Thanks
Here is the whole file of the ajax.php

    <?php
        $link=mysqli_connect("localhost","root","123456");
        mysqli_select_db($link,"demo");
         error_reporting(0);
        $make =$_GET["make"];
        $model = $_GET["model"];
        $year = $_GET["year"];
        $oem = $_GET["oem_number"];
        $lug = $_GET["lug_number"];
        $image = $_GET["lug_number"];
        
    
    if($make!="")
    {
    
        $res = mysqli_query($link,"SELECT * FROM models WHERE make_id= $make");
        echo "<select id='modeldd' onchange='change_model()'>";
        echo "<option selected = '' disabled=''>";echo "Select Model";echo "</option>";
        while($row=mysqli_fetch_array($res))
        {
            echo "<option value = '$row[model_id]'>"; echo $row["model"]; echo "</option>";
    
        }
        echo "</select>";
    }
    if($model!="")
    {
        $res = mysqli_query($link,"SELECT * FROM year WHERE model_id= $model");
        echo "<select id='yeardd' onchange='change_year()'>";
        echo "<option selected = '' disabled=''>";echo "Select Year";echo "</option>";
        while($row=mysqli_fetch_array($res))
        {
    
            echo "<option value = '$row[year_id]'>"; echo $row["year"]; echo "</option>";
    
        }
        echo "</select>";
    }
    if($year!="")
    {
        $res = mysqli_query($link,"SELECT * FROM oem WHERE year_id= $year");
        echo "<select id='oemdd' onchange='change_oem()'>";
        echo "<option selected = '' disabled=''>";echo "Select OEM number";echo "</option>";
        while($row=mysqli_fetch_array($res))
        {
    
            echo "<option value = '$row[oem_id]'>"; echo $row["oem_number"]; echo "</option>";
    
        }
        echo "</select>";
    }
    if($oem!="")
    {
        $res = mysqli_query($link,"SELECT * FROM lug_num WHERE oem_id= $oem");
        echo "<select id='lugdd' onchange='change_lug()'>";
        echo "<option selected = '' disabled=''>";echo "Select lug number";echo "</option>";
        while($row=mysqli_fetch_array($res))
        {
    
            echo "<option value = '$row[lug_id]'>"; echo $row["lug_number"]; echo "</option>";
                
        }
        echo "</select>";
    
    }
    if($lug!="")
    {
        $res = mysqli_query($link,"SELECT * FROM fix_type WHERE lug_id= $lug");
        echo "<select>";
        echo "<option selected = '' disabled=''>";echo "Select fix type";echo "</option>";
        while($row=mysqli_fetch_array($res))
        {
            $test = $row["fix_name"];
            echo "<option value = '$row[fix_id]'>"; echo $row["fix_name"]; echo "</option>";
    
        }
        echo "</select>";
    }
    $img = array();
    
    if($image!="")
    {
        $res = mysqli_query($link,"SELECT * FROM lug_num WHERE lug_id= $image");
        $array = array();
        ?><br><br><div id = img_lug><?php
        while($row=mysqli_fetch_array($res))
        {
    
            array_push($img, $row["image"]);
            //echo $img;
    
            ?><img src="<?php echo $img;  ?>"  width='200' height='200' /><?php
    
    
        }
            ?></div><?php
        
    }
    
    ?>

and the index.php

    <?php
        $link=mysqli_connect("localhost","root","123456");
        mysqli_select_db($link,"demo");
        
    ?>
    
    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
    </head>
    <body>
        <form name="form1" action="" method="post">
            <table>
                <tr>
                    <td>Select Make</td>
                    <td><select id="makedd" onchange="change_make()">
                        <option>Select</option>
                        <?php
                            $res = mysqli_query($link,"SELECT * FROM `manufacturer`");
                            while($row=mysqli_fetch_array($res))
                            {
                                ?>
                                
                                <option value="<?php echo $row["make_id"];?>"><?php echo $row["make"] ?></option>
                                <?php
                            }
                        ?>
                    </select></td>
                </tr>
                <tr>
                    <td>Select Model</td>
                    <td>
                        <div id="model">
                            <select>
                                <<option selected="" disabled="">Select Manufacturer</option>
                            </select>
                        </div>
                    </td>
    
                </tr>
                <tr>
                    <td>Select Year</td>
                    <td>
                        <div id="year">
                            <select>
                                <option>Select Year</option>
    
                            </select>
                        </div>
                    </td>
    
                </tr>
                </tr>
                <tr>
                    <td>Select OEM Number</td>
                    <td>
                        <div id="oem">
                            <select>
                                <option>Select OEM Number</option>
    
                            </select>
                        </div>
                    </td>
    
                </tr>                       
                <tr>
                    <td>Select lug Number</td>
                    <td>
                        <div id="lug">
                            <select>
                                <option>Select Lug Number</option>
    
                            </select>
                        </div>
                    </td>
    
                </tr>
                <tr>
                    <td>Select Fix type</td>
                    <td>
                        <div id="fix" >
                            <select>
                                <option>Select Fix type</option>
    
                            </select>
                        </div>
                    </td>
    
                </tr>
                <tr>
                    <td>Image from lug</td>
                    <td><div id="image">
    
                    </div>
                    </td>
    
                </tr>
                
    
            </table>
            <?php
            include "ajax.php";
            
            ?><img  src="<?php include "ajax.php"; echo $images;  ?>"  width='200' height='200'  /><?php
            ?>
            
        </form>
    <script type="text/javascript">
    function change_make(){
        var xmlhttp=new XMLHttpRequest();
        xmlhttp.open("GET","ajax.php?make="+document.getElementById("makedd").value,false);
        xmlhttp.send(null);
    
        document.getElementById("model").innerHTML=xmlhttp.responseText;
    
    }
    
    function change_model() {
        var xmlhttp=new XMLHttpRequest();
        xmlhttp.open("GET","ajax.php?model="+document.getElementById("modeldd").value,false);
        xmlhttp.send(null);
    
        document.getElementById("year").innerHTML=xmlhttp.responseText;
    }
    function change_year() {
        var xmlhttp=new XMLHttpRequest();
        xmlhttp.open("GET","ajax.php?year="+document.getElementById("yeardd").value,false);
        xmlhttp.send(null);
    
        document.getElementById("oem").innerHTML=xmlhttp.responseText;
    }
    function change_oem() {
        var xmlhttp=new XMLHttpRequest();
        xmlhttp.open("GET","ajax.php?oem_number="+document.getElementById("oemdd").value,false);
        xmlhttp.send(null);
    
        document.getElementById("lug").innerHTML=xmlhttp.responseText;
    }
    function change_lug() {
        var xmlhttp=new XMLHttpRequest();
        xmlhttp.open("GET","ajax.php?lug_number="+document.getElementById("lugdd").value,false);
        xmlhttp.send(null);
    
        document.getElementById("fix").innerHTML=xmlhttp.responseText;
    }
    
    
    </script>
    </body>
    </html>

Update so i tried with session it did not work said that i have too many sessions i tried with function same deal did not wanna work is there any way that could be done with script? Thanks

Advertisement

Answer

ajax.php

<?php
    $link=mysqli_connect("localhost","root","123456");
    mysqli_select_db($link,"demo");
     error_reporting(0);
    $make =$_GET["make"];
    $model = $_GET["model"];
    $year = $_GET["year"];
    $oem = $_GET["oem_number"];
    $lug = $_GET["lug_number"];
    $image = $_GET["lug_number"];


if($make!="")
{

    $res = mysqli_query($link,"SELECT * FROM models WHERE make_id= $make");
    echo "<select id='modeldd' onchange='change_model()'>";
    echo "<option selected = '' disabled=''>";echo "Select Model";echo "</option>";
    while($row=mysqli_fetch_array($res))
    {
        echo "<option value = '$row[model_id]'>"; echo $row["model"]; echo "</option>";

    }
    echo "</select>";
}
if($model!="")
{
    $res = mysqli_query($link,"SELECT * FROM year WHERE model_id= $model");
    echo "<select id='yeardd' onchange='change_year()'>";
    echo "<option selected = '' disabled=''>";echo "Select Year";echo "</option>";
    while($row=mysqli_fetch_array($res))
    {

        echo "<option value = '$row[year_id]'>"; echo $row["year"]; echo "</option>";

    }
    echo "</select>";
}
if($year!="")
{
    $res = mysqli_query($link,"SELECT * FROM oem WHERE year_id= $year");
    echo "<select id='oemdd' onchange='change_oem()'>";
    echo "<option selected = '' disabled=''>";echo "Select OEM number";echo "</option>";
    while($row=mysqli_fetch_array($res))
    {

        echo "<option value = '$row[oem_id]'>"; echo $row["oem_number"]; echo "</option>";

    }
    echo "</select>";
}
if($oem!="")
{
    $res = mysqli_query($link,"SELECT * FROM lug_num WHERE oem_id= $oem");
    echo "<select id='lugdd' onchange='change_lug()'>";
    echo "<option selected = '' disabled=''>";echo "Select lug number";echo "</option>";
    while($row=mysqli_fetch_array($res))
    {

        echo "<option value = '$row[lug_id]'>"; echo $row["lug_number"]; echo "</option>";

    }
    echo "</select>";

}
if($lug!="")
{
    $res = mysqli_query($link,"SELECT * FROM fix_type WHERE lug_id= $lug");
    echo "<select>";
    echo "<option selected = '' disabled=''>";echo "Select fix type";echo "</option>";
    while($row=mysqli_fetch_array($res))
    {
        $test = $row["fix_name"];
        echo "<option value = '$row[fix_id]'>"; echo $row["fix_name"]; echo "</option>";

    }
    echo "</select>";
}
$img = ""

if($image!="")
{
    $res = mysqli_query($link,"SELECT * FROM lug_num WHERE lug_id= $image");
    $array = array();
    ?><br><br><div id = img_lug><?php
    while($row=mysqli_fetch_array($res))
    {

        $img = $row["image"];
        //echo $img;

        ?><img src="<?php echo $img;  ?>"  width='200' height='200' /><?php


    }
        ?></div>
<?php } ?>

index.php

<?php
    $link=mysqli_connect("localhost","root","123456");
    mysqli_select_db($link,"demo");

?>

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <form name="form1" action="" method="post">
        <table>
            <tr>
                <td>Select Make</td>
                <td><select id="makedd" onchange="change_make()">
                    <option>Select</option>
                    <?php
                        $res = mysqli_query($link,"SELECT * FROM `manufacturer`");
                        while($row=mysqli_fetch_array($res))
                        {
                            ?>

                            <option value="<?php echo $row["make_id"];?>"><?php echo $row["make"] ?></option>
                            <?php
                        }
                    ?>
                </select></td>
            </tr>
            <tr>
                <td>Select Model</td>
                <td>
                    <div id="model">
                        <select>
                            <<option selected="" disabled="">Select Manufacturer</option>
                        </select>
                    </div>
                </td>

            </tr>
            <tr>
                <td>Select Year</td>
                <td>
                    <div id="year">
                        <select>
                            <option>Select Year</option>

                        </select>
                    </div>
                </td>

            </tr>
            </tr>
            <tr>
                <td>Select OEM Number</td>
                <td>
                    <div id="oem">
                        <select>
                            <option>Select OEM Number</option>

                        </select>
                    </div>
                </td>

            </tr>                       
            <tr>
                <td>Select lug Number</td>
                <td>
                    <div id="lug">
                        <select>
                            <option>Select Lug Number</option>

                        </select>
                    </div>
                </td>

            </tr>
            <tr>
                <td>Select Fix type</td>
                <td>
                    <div id="fix" >
                        <select>
                            <option>Select Fix type</option>

                        </select>
                    </div>
                </td>

            </tr>
            <tr>
                <td>Image from lug</td>
                <td><div id="image">

                </div>
                </td>

            </tr>


        </table>
        <?php
        include "ajax.php";

        ?><img src="<?php include "ajax.php"; echo $img;  ?>"  width='200' height='200'  /><?php
        ?>

    </form>
<script type="text/javascript">
function change_make(){
    var xmlhttp=new XMLHttpRequest();
    xmlhttp.open("GET","ajax.php?make="+document.getElementById("makedd").value,false);
    xmlhttp.send(null);

    document.getElementById("model").innerHTML=xmlhttp.responseText;

}

function change_model() {
    var xmlhttp=new XMLHttpRequest();
    xmlhttp.open("GET","ajax.php?model="+document.getElementById("modeldd").value,false);
    xmlhttp.send(null);

    document.getElementById("year").innerHTML=xmlhttp.responseText;
}
function change_year() {
    var xmlhttp=new XMLHttpRequest();
    xmlhttp.open("GET","ajax.php?year="+document.getElementById("yeardd").value,false);
    xmlhttp.send(null);

    document.getElementById("oem").innerHTML=xmlhttp.responseText;
}
function change_oem() {
    var xmlhttp=new XMLHttpRequest();
    xmlhttp.open("GET","ajax.php?oem_number="+document.getElementById("oemdd").value,false);
    xmlhttp.send(null);

    document.getElementById("lug").innerHTML=xmlhttp.responseText;
}
function change_lug() {
    var xmlhttp=new XMLHttpRequest();
    xmlhttp.open("GET","ajax.php?lug_number="+document.getElementById("lugdd").value,false);
    xmlhttp.send(null);

    document.getElementById("fix").innerHTML=xmlhttp.responseText;
}


</script>
</body>
</html>
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement