Dear community,

May I ask you for your help please.

I have two pages: item page, where customer selects required size and basket page, where this size is displayed alongside other information. All the data is passed OK, but 'size' data is not.

Here are the pieces of code that created a small headache for me for a few days:

item page:

<?php while($row=mysql_fetch_array($result)) { ?>
            <div id="gallery">
                <a href="Images/<?php echo $row['image_URL']; ?>" title="<?php echo $row['item_name']; ?>"><img src="Images/<?php echo $row['image_URL']; ?>" width="214" height="308" alt="<?php echo $row['item_name']; ?>" align="left" /></a>
                <a href="Images/<?php echo $row['screen1_URL']; ?>" title="<?php echo $row['item_name']; ?>"><img src="Images/<?php echo $row['screen1_URL']; ?>" width="92" height="132" alt="<?php echo $row['item_name']; ?>" align="left" /></a>            
                <a href="Images/<?php echo $row['screen2_URL']; ?>" title="<?php echo $row['item_name']; ?>"><img src="Images/<?php echo $row['screen2_URL']; ?>" width="92" height="132" alt="<?php echo $row['item_name']; ?>" align="left" /></a>
            </div>
            <div class="item">
                <p class="gallery-indivitem"><?php echo $row['item_name']; ?></p>

                <p class="gallery-blacksmall"> <?php echo $row['item_price']; ?> рублей</p><br /><br />
                <p class="gallery-blacksmall"><?php echo $row['long_description']; ?></p>                            
                 <p class="gallery-blacklarge">Выбрать размер
                     <select name="sel_size" id="sel_size">
                         <option value="one"><?php echo $row['size']; ?></option>
                         <option value="two"><?php echo $row['size1']; ?></option>
                         <option value="3"><?php echo $row['size2']; ?></option>
                         <option value="4"><?php echo $row['size3']; ?></option>
                         <option value="5"><?php echo $row['size4']; ?></option>
                     </select>
                 </p>
                 <a class="smallblue" href="#" >Помощь с размером</a><br />
                 <a class="smallblue" href="javascript: history.back();" >Вернуться</a><br /><br />
                 <a href="busket.php?action=add&id=<?php echo $row['item_id']; ?>"><button type="submit" class="button" name="submit1" value="submit"></button> </a>
                 <?php } ?>

basket page:

foreach ($contents as $id=>$qty) {
            $sql = 'SELECT * FROM `ITEM` WHERE `item_id` = '.$id;
            $cartresult=mysql_query($sql);


            while($cartrow = mysql_fetch_array($cartresult)) {
                $output[] = '<tr>';
                $output[] = '<td>'.$cartrow['item_name'].'</td>';
                $output[] = '<td>'.$cartrow['sel_size'].'</td>';
                $output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="2" maxlength="2" /></td>';
                $output[] = '<td><p class="small">'.($cartrow['item_price'] * $qty).'</p></td>';
                $output[] = '<td><a class="smallblueagain" href="busket.php?action=delete&id='.$id.'">удалить</a></td>';
                $total += $cartrow['item_price'] * $qty;
                $_POST['total'] = $total;
                $output[] = '</tr>';

The problem is that when I add any 'name' to the select box, it does not pass any size information to the basket page, however, if I change the 'name' of the select box to any 'name' from the available options such as size, size1, size2 etc, it only displays that particular data associated with that size.
I think the issue is with this two pieces of code (see below), however I am not sure how to change this and make the selected size be correctly displayed on the basket page.

<select name="sel_size" id="sel_size">

insert page

 $output[] = '<td>'.$cartrow['sel_size'].'</td>';

busket page

Do I need to create a variable for the size or maybe my database table is constructed badly?
Thank you for any help,
Annushka

Member Avatar for LastMitch

@anna.piperides

Do I need to create a variable for the size or maybe my database table is constructed badly?

Did you write this script?

There's something wrong here:

 <select name="sel_size" id="sel_size">
 <option value="one"><?php echo $row['size']; ?></option>
 <option value="two"><?php echo $row['size1']; ?></option>
 <option value="3"><?php echo $row['size2']; ?></option>
 <option value="4"><?php echo $row['size3']; ?></option>
 <option value="5"><?php echo $row['size4']; ?></option>
 </select>

As each item in the shop might have up to five size options (sometimes it's only one size, but sometimes an item is available in three sizes etc.) I have created five different fields for size info in my database table for ITEM (other information include item_id, item_type etc)

So the problem is that the code does not pass the selected size option.

To demonstarate what I mean here are the links to these two pages that I am working on:

insert page:

http://www.piperidesigns.com/retro-vixens-russia/retro-vixens-itempage.php?id=1

basket page:

http://www.piperidesigns.com/retro-vixens-russia/busket.php?action=add&id=1

I think there might be a problem with the database design, but if someone knows a different code or an answer how I can add the selected option to the basket it will be super.

thank you.

Anna, it's hard to work out what you actually want to achieve. It seems to me that you want to grab the selected size from the add to basket page and then carry that size onto the basket page and show it there also. In which case, you simply need to grab the $_POST['sel_size']/$_GET['sel_size'].
Use the $row['size'] for both the value and the user's text choice -

<select name="sel_size" id="sel_size">
<option value="<?php echo $row['size']; ?>"><?php echo $row['size']; ?></option>
<option value="<?php echo $row['size1']; ?>"><?php echo $row['size1']; ?></option>
</select>

If you want the variable available to javascript on the basket page, create a hidden variable in PHP on page load: <input type="hidden" id="selected_size" value="<?php echo $_POST['sel_size'];?">
Then access it using document.getElementById('selected_size').value
If I have misunderstood and told you something you learnt at kindergarten, please accept my apologies and ignore me :D

Thank you Adam, you are right, I need to grab the selected size and pass it onto the basket page.

Where do I put

$_POST['sel_size']/$_GET['sel_size']

.
($_POST['sel_size']) goes in the header of insert page and ($_GET['sel_size']) basket page, is it right ?

This is the beggining of the code I have on the basket page. Do I need to stick ($_GET['sel_size']) just after

$contents = array();

code at the very beggining?



function showCart() {
    $cart = $_SESSION['cart'];

    if ($cart) {
        $items = explode(',',$cart);
        $contents = array();
        foreach ($items as $item) {
            $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
        }
        $output[] = '<form action="busket.php?action=update" method="post" id="cart">';
        $output[] = '<div id="middle_index2">';
        $output[] = '<table>';
        $output[] = '<tr>';
        $output[] = '<th colspan="2" class="redbig" style="border-bottom:#cccccc solid 1px">Ваша корзина</th>';
        $output[] = '<th colspan="3" style="text-align:right; border-bottom:#cccccc solid 1px"><a class="smallreturnlink" href="index.php" target="_blank" align="right">Продолжить покупки >></a></th>';
        $output[] = '</tr>';
        $output[] = '<tr>';
        $output[] = '<td><p class="smallorder">Выбранная вещь</p></td>';
        $output[] = '<td><p class="smallorder">Размер</p></td>';
        $output[] = '<td><p class="smallorder">Количество</p></td>';
        $output[] = '<td><p class="small" style="color:#FF0000">Цена</p></td>';
        $output[] = '<td><p class="small" style="color:#FF0000">Удалить</p></td>';
        $output[] = '</tr>';
        $output[] = '<tr>';
        foreach ($contents as $id=>$qty) {
            $sql = 'SELECT * FROM `ITEM` WHERE `item_id` = '.$id;
            $cartresult=mysql_query($sql);


            while($cartrow = mysql_fetch_array($cartresult)) {
                $output[] = '<tr>';
                $output[] = '<td>'.$cartrow['item_name'].'</td>';
                $output[] = '<td>'.$cartrow['sel_size'].'</td>';
                $output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="2" maxlength="2" /></td>';
                $output[] = '<td><p class="small">'.($cartrow['item_price'] * $qty).'</p></td>';
                $output[] = '<td><a class="smallblueagain" href="busket.php?action=delete&id='.$id.'">удалить</a></td>';
                $total += $cartrow['item_price'] * $qty;
                $_POST['total'] = $total;
                $output[] = '</tr>';

Thank you for your help.

Hey Anna, I'm still not sure what you want to achieve, but I think it is this line you need to change:

$output[] = '<td>'.$cartrow['sel_size'].'</td>';

to:

$output[] = '<td>'.$_POST['sel_size'].'</td>';

The script is trying to get the selected size from the database but the information has not been stored there yet? Rather the selected size is still in the $_POST array, presuming the 'method="post"' in the add to basket form.

Dear Adam,

I think the problem is between these two pieces of code.
The information from the select box

<select name="sel_size" id="sel_size">
<option value="one"><?php echo $row['size']; ?></option>
<option value="two"><?php echo $row['size1']; ?></option>
<option value="3"><?php echo $row['size2']; ?></option>
<option value="4"><?php echo $row['size3']; ?></option>
<option value="5"><?php echo $row['size4']; ?></option>
</select>

I changed the code above to your suggestion, which I think makes perfect sense:

<select name="sel_size" id="sel_size">
<option value="<?php echo $row['size']; ?>"><?php echo $row['size']; ?></option>
<option value="<?php echo $row['size1']; ?>"><?php echo $row['size1']; ?></option>
</select>

is not passed to this line in the basket page

 $output[] = '<td>'.$cartrow['sel_size'].'</td>';

changing the piece of code above to $output[] = '<td>'.$_POST['sel_size'].'</td>'; did not help to solve the problem.

When I looked through other posts with similar problems, there are some suggestions to create an array
<select name="sel_size[]" id="sel_size">. However I am not sure if it's applicable in this case.

I have other similar code working, which adds selected option to a database, however I cannot work it out how to pass data to the next page in this case.

Hey Anna. I duplicated your select element and button:

<?php
if ($_POST){print_r($_POST);}
$row = array('size'=>"one", 'size1'=>"two", 'size2'=>"three", 'size3'=>"four", 'size4'=>"five" );
?>
<form name="form" method="post" action="">
<select name="sel_size" id="sel_size">
<option value="<?php echo $row['size']; ?>"><?php echo $row['size']; ?></option>
<option value="<?php echo $row['size1']; ?>"><?php echo $row['size1']; ?></option>
<option value="<?php echo $row['size2']; ?>"><?php echo $row['size2']; ?></option>
<option value="<?php echo $row['size3']; ?>"><?php echo $row['size3']; ?></option>
<option value="<?php echo $row['size4']; ?>"><?php echo $row['size4']; ?></option>
</select>
<input name="submit" type="submit" value="submit">
<form>

When I choose a option then press the button, the page reloads and echos:

Array ( [sel_size] => four [submit] => submit ) 

Which is $_POST['sel_size'] with a value of 'four'.
The use of <select name="array[]"> is only for multi-select form elements.
I presume you are working with somebody else's script? Is the selected size supposed to be entered into a DB and all the current basket details taken from the DB? In which case, the selected size needs to be inserted into the database first.

Member Avatar for LastMitch

@anna.piperides

All the data is passed OK, but 'size' data is not

This is your code:

<select name="sel_size" id="sel_size">
<option value="one"><?php echo $row['size']; ?></option>
<option value="two"><?php echo $row['size1']; ?></option>
<option value="3"><?php echo $row['size2']; ?></option>
<option value="4"><?php echo $row['size3']; ?></option>
<option value="5"><?php echo $row['size4']; ?></option>
</select>

Try this:

<select name="sel_size" id="sel_size">
<option value="one"><?php echo $row['size']; ?></option>
<option value="two"><?php echo $row['size1']; ?></option>
<option value="three"><?php echo $row['size2']; ?></option>
<option value="four"><?php echo $row['size3']; ?></option>
<option value="five"><?php echo $row['size4']; ?></option>
</select>

or this:

<select name="sel_size" id="sel_size">
<option value="1"><?php echo $row['size']; ?></option>
<option value="2"><?php echo $row['size1']; ?></option>
<option value="3"><?php echo $row['size2']; ?></option>
<option value="4"><?php echo $row['size3']; ?></option>
<option value="5"><?php echo $row['size4']; ?></option>
</select>

Either option is good, I'm not sure what is on the DB either it's '1' or 'one'?

Now you're output there's an error:

 $output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="2" maxlength="2" /></td>';

LastMitch and Adam, thank you for your help! I really appreciate it!

Hey, no problem, but did you get it working?

Member Avatar for LastMitch

@anna.piperides

Did you fixed the issue?

I did not fix the issue, but I changed the size selection to one option only, so I leave it like this for now until I give it to my web developer to fix it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.