简体   繁体   中英

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

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>

The way that you trying is wrong. Look, your $image variable can be global but your loop is not global. It will give you the last value of loop.

1.Change $img variable to local.
2.Then create a new global variable. And give it the values like this.

global $global_images;
$global_images.= "@".$row["image"]; // @ is just a separator.

3.Now on the other page (where you want), you have to create basic loop again, like this

include "2.php";

$get_images=explode('@', $global_image);  //We can choose one by one the images with @ and explode function
//The loop
foreach ($get_images as $show){?>
<img src="<?php echo $show;  ?>"  width='200' height='200'>
<?php } ?>

As I understand it, this is exactly what you want. Now you can customize it on demand.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM