Jump to content

Use checkbox to send row value to cart php


jamesreno20

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>

 

Link to comment
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

Link to comment
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.

Edited by mik_se7
Link to comment
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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.