Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

How to pass a php variable to a popup window

Featured Replies

Hello,

 

I am working on an online store. Each product has QUICK VIEW link which popup the product detail in a popup window. But am finding it difficult to make the popup show the detail of the particular product clicked. Below are my codes.

$get_items = mysqli_query($my_link, "SELECT * FROM $table ORDER BY id DESC);
while($row=mysqli_fetch_array($get_items)){

$id = stripslashes($row[id]);
$category = stripslashes($row[category]);
$type = stripslashes($row[type]);
$product = stripslashes($row[name]);

echo "<a href='detail.php?id=$id'>".$product."</a>";
echo $category
<a class="preview" href="#product-preview" data-toggle="modal" ></a>

}

/* I REPLACED
<a class="preview" href="#product-preview" data-toggle="modal" ></a>

WITH
<a class="preview" href="id=$id#product-preview" data-toggle="modal" ></a>

AND ALSO
<a class="preview" href="#product-preview&id=$id" data-toggle="modal" ></a>

BUT DIDN'T WORK
*/

Below is my popup code

<?php
$id = $_GET[id];

$get = mysqli_query($my_link, "SELECT * from products WHERE id='$id'");
$row = mysqli_fetch_array($get);
$id = stripslashes($row[id]);
$category = stripslashes($row[category]);
$product = stripslashes($row[name]);

?>

<div class="modal fade" id="product-preview" tabindex="-1" role="dialog" aria-hidden="true">
                <div class="modal-dialog modal-lg">
                    <div class="modal-content space-40">
                        <a aria-hidden="true" data-dismiss="modal" class="sb-close-btn close fa fa-times" href="#"></a>
                        <!--<button aria-hidden="true" data-dismiss="modal" class="close sb-close-btn" type="button"><i class="fa fa-times"></i></button>-->
                        <!-- Single Products Start -->
                        <div class="clearfix"></div>
                        <section id="product-fullwidth" class="clearfix"> 
                            <div class="single-product-wrap">      
                                <div class="list-category-details">                               
                                    <div class="col-md-7 col-sm-7">                                  
                                        <!-- Main Slider Start -->
                                        <section id="main-slider1" class="carousel slide main-slider">  
                                            <!--Carousel Slide Button Start-->
                                            <div class="slider-pagination col-md-2 col-sm-3  col-xs-3">                
                                                <ul class="product-thumbnails">
                                                    <li  data-slide-to="0" data-target="#main-slider1">
                                                        <a href="#"><img class="img-responsive" alt="img" src="assets/img/product/single-thumb1.png"></a></li>
                                                    <li class="active" data-slide-to="1" data-target="#main-slider1">
                                                        <a href="#"><img class="img-responsive" alt="img" src="assets/img/product/single-thumb2.png"></a></li>
                                                    <li data-slide-to="2" data-target="#main-slider1">
                                                        <a href="#"><img class="img-responsive" alt="img" src="assets/img/product/single-thumb3.png"></a></li> 
                                                    <li data-slide-to="3" data-target="#main-slider1">
                                                        <a href="#"><img class="img-responsive" alt="img" src="assets/img/product/single-thumb4.png"></a></li> 
                                                </ul>  
                                            </div>    
                                            <div class="col-md-10 col-sm-9  col-xs-9">
                                                <div class="carousel-inner product-fullwidth light-bg slider">
                                                    <div class="item">  
                                                        <img src="assets/img/product/single-prod1.png" alt="...">
                                                    </div>     
                                                    <div class="item active">  
                                                        <img src="assets/img/product/single-prod1.png" alt="...">  
                                                    </div> 
                                                    <div class="item">  
                                                        <img src="assets/img/product/single-prod1.png" alt="..."> 
                                                    </div> 
                                                    <div class="item">  
                                                        <img src="assets/img/product/single-prod1.png" alt="..."> 
                                                    </div> 
                                                </div>
                                            </div>

                                        </section>
                                        <!-- / Main Slider Ends -->                                      
                                    </div>
                                    <div class="col-md-5 col-sm-5">
                                        <div class="product-content">
                                            <div class="rating">                                                              
                                                <span class="star active"></span>
                                                <span class="star active"></span>
                                                <span class="star active"></span>
                                                <span class="star half"></span>
                                                <span class="star"></span>
                                                <div class="product-review">
                                                    <ul>
                                                        <li>03 Review </li>
                                                        <li> | <a href="#"> Add Your Review </a> </li>
                                                    </ul>
                                                </div>
                                            </div>                                            
                                            <div class="product-name">
                                                <a href="#"><?php echo $product; ?></a>                                              
                                            </div>
                                            <div class="product-price">
                                                <h4 class="pink-btn-small price-line"> $50.00 </h4>
                                                <h4 class="blue-btn-small"> $45.00 </h4>
                                            </div>
                                            <div class="product-availability">
                                                <ul class="stock-detail">
                                                    <li>Available:<strong class="green-color"> <i class="fa fa-check-circle"></i> In Stock </strong> | </li>
                                                    <li>Product Id:<?php echo $_GET[pid]; ?><strong> #201546 </strong> </li>
                                                </ul>
                                                <hr class="fullwidth-divider">                                            
                                            </div>
                                            <div class="product-size">
                                                <form class="product-form">
                                                    <div class="row">
                                                        <div class="form-group selectpicker-wrapper">
                                                            <label>SIZE</label>
                                                            <select title="Looking to Buy" data-toggle="tooltip" data-width="100%" data-live-search="true" class="selectpicker input-price bs-select-hidden"><option class="bs-title-option" value="">2 - 4 Years</option>
                                                                <option>4 - 6 Years</option>
                                                                <option>6 - 8 Years</option>
                                                                <option>8 - 10 Years</option>
                                                            </select>
                                                        </div>
                                                        <div class="form-group selectpicker-wrapper">
                                                            <label>QTY</label>
                                                            <select title="Looking to Buy" data-toggle="tooltip" data-width="100%" data-live-search="true" class="selectpicker input-price bs-select-hidden"><option class="bs-title-option" value="">02</option>
                                                                <option>03</option>
                                                                <option>04</option>
                                                                <option>05</option>
                                                            </select>
                                                        </div>
                                                    </div>
                                                </form>
                                                <div class="widget-colors">
                                                    <span>Colors:</span>
                                                    <label class="rcheckbox-inline"><input type="checkbox" value="" id="brown"> <span></span></label> 
                                                    <label class="rcheckbox-inline"><input type="checkbox" value="" id="light"> <span></span></label> 
                                                    <label class="rcheckbox-inline"><input type="checkbox" value="" id="dark"> <span></span></label> 
                                                    <label class="rcheckbox-inline"><input type="checkbox" value="" id="orange"> <span></span></label> 
                                                    <label class="rcheckbox-inline"><input type="checkbox" value="" id="blue"> <span></span></label> 
                                                    <label class="rcheckbox-inline"><input type="checkbox" value="" id="yellow"> <span></span></label> 
                                                    <label class="rcheckbox-inline"><input type="checkbox" value="" id="green"> <span></span></label> 
                                                    <label class="rcheckbox-inline"><input type="checkbox" value="" id="white"> <span></span></label> 
                                                </div>
                                                <hr class="fullwidth-divider">   
                                            </div>                                            
                                            <div class="add-to-cart">
                                                <a class="blue-btn btn" href="#"> <i class="fa fa-shopping-cart white-color"></i> Add to Cart</a>
                                                <a class="pink-btn btn" href="product-single-fullwidth.html"> Go To Details </a>
                                            </div>
                                        </div>
                                    </div>
                                </div>     
                            </div>            
                        </section>
                        <!-- Single Products Ends -->
                    </div>
                </div>
            </div>

I did not read through all your code but it looks like you are listing variables in a string format

echo "<a href='detail.php?id=$id'>".$product."</a>";

should be

echo "<a href='detail.php?id=".$id."'>".$product."</a>";

otherwise $id will just be parsed as "$id" (literal) and not the actual value of the variable

Edited by Nillervision

Why create a new window? Just use some UI in the current page like a modal and ajax the new data in. Or my new window do you actually mean a modal and not a window as your code suggests?

Edited by rbrtsmith

  • Author

Thanks Nillervision & rbrtsmith,

 

@ Nillervision: That's not even the problem because it's not affecting it.

@ rbrtsmith: am not opening another window, what i meant was modal. Pls am not advance in modal & ajax. can you please put me through how to go about that.

Have you checked that $project has data stored within it?

 

I would var_dump(); it straight after it's assigned and check it has the data you are expecting. Because you're using a modal form as long as the variables are set before the modal code you should be able to reference the php variable from within the modal.

 

The simplest way would be to set the variables on page load.

Hmm had a read of your code, I'm a touch confused, is all that code in one page or split across 2? Are you using js to open this window (pop over)?

<?php 
$get_items = mysqli_query($my_link, "SELECT * FROM $table ORDER BY id DESC"); // Added Missing " 
while($row=mysqli_fetch_array($get_items)){
    $id = stripslashes($row[id]); // Not an int? e.g. $id = intval($row[id]);
    $category = stripslashes($row[category]);
    $type = stripslashes($row[type]);
    $product = stripslashes($row[name]);

   echo "<a href=\"detail.php?id=" . $id . "\">" . $product . "</a>"; // You can use " if you escape it.
   echo $category;
   echo "<a class=\"preview\" href=\"#" . $id . "\" data-toggle=\"modal\"></a>";
}

Using the above should make the preview button have an href="#product_id" so maybe href="#34". Now it has an href you can access the href with js which will determine which product they want.

 

I assume this is a long product list with one 'window' for popup preview? Is so and you're making a list, something like...

<ul>
<li id="34"><a href="detail.php?id=34" class="name">Product One</a><br>Category<br><a href="#34" class="preview">Preview</a></li>
<li id="35"><a href="detail.php?id=35" class="name">Product Two</a><br>Category<br><a href="#35" class="preview">Preview</a></li>
</ul>

Then you can access the href and content of any parts inside the container id? The main question is what's the difference between $table and 'products', are they different or the same?

Edited by BrowserBugs

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.