jamesreno20 Posted November 20, 2014 Share Posted November 20, 2014 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> Quote Link to comment Share on other sites More sharing options...
mik_se7 Posted November 20, 2014 Share Posted November 20, 2014 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? Quote Link to comment Share on other sites More sharing options...
jamesreno20 Posted November 20, 2014 Author Share Posted November 20, 2014 it doesnt ouput anything im not sure how you get the values from each row from teh checkbox to display to the basket Quote Link to comment Share on other sites More sharing options...
mik_se7 Posted November 20, 2014 Share Posted November 20, 2014 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 Quote Link to comment Share on other sites More sharing options...
mik_se7 Posted November 20, 2014 Share Posted November 20, 2014 (edited) 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 November 20, 2014 by mik_se7 Quote Link to comment Share on other sites More sharing options...
davidannis Posted November 20, 2014 Share Posted November 20, 2014 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.