Jump to content

Archived

This topic is now archived and is closed to further replies.

jamesreno20

Use checkbox to send row value to cart php

Recommended Posts

so i have my database form which you can select a some data using the checkbox and when submitting it should get the row info and display it on the next page iv been struggling for days on this and cant work out how to do it please could i have some help

<form name="test" method="post" action="shoppingbasket.php" >

<input type="submit" value="Submit" >
<?php
     $conn = pg_connect("host=db.dcs.aber.ac.uk port=5432 
 dbname=teaching user=csguest password=r3p41r3d");
      $res = pg_query ($conn, "SELECT artist,composer,genre,title,album,label,price,description FROM music");
echo "<table border='1'>";
echo "<tr><th>Select</th><th>Artist</th><th>Composer</th><th>Genre</th><th>Title</th><th>Album</th><th>Label</th><th>Price</th><th>Description</th></tr>";
 while ($row = pg_fetch_row($res)) 
 {
 echo "<tr>";
 echo '<td><input type="checkbox" name="check_list[]" value="' . $row['ref'] . $row['artist'] .  '"></td>';
 for ($column = 0; $column < pg_num_fields($res); $column++) 
 {
 echo "<td>" . $row[$column] . "</td>";
 }
 echo "</tr>";
 }
 echo "</table>\n";
___________________________________________________________________________________________________
shopping basket.php
 
<?php
 
echo "<table border='1'>";
echo "<tr><th>Select</th><th>Artist</th><th>Composer</th><th>Genre</th><th>Title</th><th>Album</th><th>Label</th><th>Price</th><th>Description</th></tr>";
 
 if(!empty($_POST['check_list'])){
     foreach($_POST['check_list'] as $row){
        echo "$row was checked! ";
     }
   }
?>
</form>
 </body>
 </html>

 

Share this post


Link to post
Share on other sites

what happens with the code as it is does it give you an error message or not display all the fields? does it go onto the next page?

Share this post


Link to post
Share on other sites

it doesnt ouput anything im not sure how you get the values from each row from teh checkbox to display to the basket

Share this post


Link to post
Share on other sites

I used this php code for check boxes i had on a form i created:

$variable = (isset($_POST['artist']) && $_POST['artist'] == 'Yes');

or you might find javascript more helpful:

function getArtist(){
        var artist =[];
        var art = document.getElementsByName("artist[]");
        for(var m=0; m<art.length; m++)
          if(art[m].checked){
            artist.push(art[m].value);
            alert("the artist is "+artist);
            }
}

the javascript and php may require some work to fit your code and the javascript will require ajax code to send the data back to php for you to add to your database if required.

you can remove or // the alert i just put that there to help you see if you get the right value your expecting

Share this post


Link to post
Share on other sites

not sure if you need this or might find it helpful:

 

a check box on my form:

<div id="table2a">
        artist:<input id="checkboxes[]" name="artist" type="checkbox" value="Yes" onfocus="emptyElement('status')" maxlength="10">
</div>

and more php code for you if you want to get the data for your database after the $variable i gave you before:

$artist = mysqli_real_escape_string($db_conx, $artist);

$db_conx is a connection file to my database which is more secure than posting your connection details in a support post and if anyone checks out your source code.

Share this post


Link to post
Share on other sites

In the script to which you submit, you will have a multidimensional array $_POST['check_list'][] with the values. To see what you have try this:

echo "<pre>\n";
print_r($_POST['check_list']);
echo "</pre>\n";

To process each value in the array $_POST['check_list'] use

foreach ($_POST['check_list'] as $value){
//do whatever here
}

http://php.net/manual/en/control-structures.foreach.php

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.