loxfear Posted June 23, 2014 Share Posted June 23, 2014 this is my code: <?php include 'sqlconnect.php'; $sql = mysqli_query($con,"SELECT * FROM aktiviteter"); $data = array(); while ($row = mysqli_fetch_assoc($sql)) { $data[$row['id']] = array( 'title' => $row['title'], 'pris' => $row['pris'], 'beskrivelse' => $row['beskrivelse'], ); } print_r(error_get_last()); ?> <html> <head> <title>Polterplanner Bestilling</title> <link rel="stylesheet" type="text/css" href="style.css"> <script> var jsArray = []; <?php foreach($data as $key => $value): echo 'jsArray["'.$key.'"] = [];'; echo "\r\n"; foreach($value as $infoType => $info): echo 'jsArray["'.$key.'"]["'.$infoType.'"] = "'.$info.'";'; echo "\r\n"; endforeach; endforeach; print_r(error_get_last()); ?> function activitySelectionChanged(elementID) { var activitySelect = document.getElementById('activity' + elementID); var selectedValue = activitySelect.value; var priceOutputBox = document.getElementById('activityPrice' + elementID); priceOutputBox.innerHTML = jsArray[selectedValue]["pris"]; var price1 = document.getElementById('activityPrice').innerHTML; var price2 = document.getElementById('activityPrice2').innerHTML; var total = document.getElementById('activityTotal'); if(price1!='Pris' && price2!='Pris') { total.innerHTML = parseInt(price1) + parseInt(price2); } } </script> </head> <body> <div id"wrapper"> <div id="tableWrapper"> <table class="tables" width="349" height="27" border="0"> <tr> <td width="174" height="23"> <select class="styled-select" name="activity" id="activity" onChange="activitySelectionChanged('')"> <option value="">-----------------</option> <?php foreach($data as $key => $value): echo '<option value="'.$key.'">'.$value['title'].'</option>'; echo "\r\n"; endforeach; print_r(error_get_last()); ?> </select></td> <td width="86"> </td> <td width="75"><span class="Pris" id="activityPrice">Pris</span>,-</td> </tr> </table> <table class="tables" width="349" height="27" border="0"> <tr> <td width="174" height="23"> <select class="styled-select" name="activity2" id="activity2" onChange="activitySelectionChanged(2)"> <option value="">-----------------</option> <?php foreach($data as $key => $value): echo '<option value="'.$key.'">'.$value['title'].'</option>'; echo "\r\n"; print_r(error_get_last()); endforeach; ?> </select></td> <td width="86"> </td> <td width="75"><span class="Pris" id="activityPrice2">Pris</span>,-</td> </tr> </table> <table class="tables" width="349" height="27" border="0"> <tr> <td width="174" height="27">Total:</td> <td width="86"> </td> <td width="75"><span class="Pris" id="activityTotal">Total</span>,-</td> </tr> </table> </div> <!-- tableWrapper ends --> </div> <!-- wrapper ends --> </body> </html> what i want is the info send through a form, so that im ale to send this via an email. would i use: document.getElementById( ...... ); ? link to working page: http://polterplanner.dk/bestiller.php Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted June 23, 2014 Share Posted June 23, 2014 It's unclear what you want, which is probably why nobody hasn't answered yet. What do you mean by “send info through a form”? Do you want to submit the selected options to another script? Then why not use an actual form? That's what they're for. Generating dynamic JavaScript code is actually a very bad idea, because you easily end up with cross-site scripting vulnerabilities or plain bugs. For example, if there's any single quote in your data, your whole script blows up with a syntax error. That's hardly a good solution. Instead, simply store the price in a data attribute in the option element itself. Or load the data with an Ajax request. Or JSON-encode the data, HTML-escape it and then put it into a hidden div element. But do not drop PHP values into a script element. That's really the worst of all approaches. Quote Link to comment Share on other sites More sharing options...
loxfear Posted June 23, 2014 Author Share Posted June 23, 2014 Thank you, haha i should probably relook at the form ;0 but doest the values have to be inside a text input to be able to send it out? Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted June 23, 2014 Share Posted June 23, 2014 The keys are already in the value attribute of the option. They're sent automatically. Or are you talking about the price, the title etc.? You do not need to send those. You already have them in your database and can look them up at any time, so all you need to send is the key. Quote Link to comment Share on other sites More sharing options...
loxfear Posted June 24, 2014 Author Share Posted June 24, 2014 omg .. your right .. why didnt i think of that :/ Quote Link to comment Share on other sites More sharing options...
loxfear Posted July 1, 2014 Author Share Posted July 1, 2014 How do i send the key? Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted July 1, 2014 Share Posted July 1, 2014 What's the concrete issue? Understanding how forms in general work? Using the submitted value to look up all the other data? Something else? I can't help you based on such a vague question. Quote Link to comment Share on other sites More sharing options...
loxfear Posted July 1, 2014 Author Share Posted July 1, 2014 (edited) Using the submitted value to look up all the other data is the biggest issue, and how to submit the data i need, or is the selecter able to do that? ps. sorry for the vague question Edited July 1, 2014 by loxfear Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted July 1, 2014 Share Posted July 1, 2014 You simply use the activity IDs as the values of the select element. When the user chooses an activity, the corresponding ID gets submitted. On the target page, you can then fetch any activity data you want by making a query with a WHERE clause. 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.