-1

I am developing a web application and i am stuck with a problem. i have two drop-down list, one is dynamically populated from my db, while the the other is static html.

When a user selects an item form the dynamic drop-down and select an item from the static html drop-down, and clicks submit on the form, I would like the dynamic selected list item to print out the entire selected row in the db.

here is my code sample

if ($contactc =="Yes"){

$result = mysql_query("SELECT * FROM company_details where comid = '$selectsector'");

while ($row = mysql_fetch_array($result)){
echo $row["fname"]." ".$row["comname"]."<br/>";
}


}
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
eugbana
  • 37
  • 8
  • 3
    If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Aug 28 '15 at 19:02
  • [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Aug 28 '15 at 19:02
  • it involves the html `onchange` attribute of the ` – developerwjk Aug 28 '15 at 19:04
  • Is your current code is not working ? If yes then what part of it is not working ? – Maximus2012 Aug 28 '15 at 20:05

1 Answers1

0

maybe this is what you wanted, though you will have to use JQuery

HTML

<form id="when_user_clicks_submit_button">
<input class="data_to_send" type="text" value="MySQL Selection data"></input>   
<input type="submit" value="Submit"></input>
</form>

JavaScript|JQuery for Ajax Request:

document.getElementById('when_user_clicks_submit_button').onsubmit = function() {
$.ajax({
type: "POST",
url: "php_code_script.php",//php script location
data: { "data_from_form": data_to_send }, //data to send in JSON format
success: function(data) { //callback if the script execution is successful
$('<p>' + data + '</p>').appendTo('body');//this returns the selected information from the MySQL database and inserts it somewhere, make sure to echo or print the selected data inside the php script.
}
)}
};

PHP

    if($_POST['data_from_form']) {
    if ($contactc =="Yes"){
$selectsector = $_POST['data_from_form']; //gets the sended data from the form page to use for selection in the MySQL database

    $result = mysqli_query("SELECT * FROM company_details where comid = '$selectsector'");

    while ($row = mysqli_fetch_array($result)){
    echo $row["fname"]." ".$row["comname"]."<br/>";
    }
    }
    }

Maybe this is sort of what you wanted, you basically send the information you want to use to select from the database, the php script then handles it and echoes the selected information from the database, that data then returns to the form page where it becomes the value pf an DOM Element(HTML tag). This is just to give an example of what you maybe want, I didn't think about security or anything(I don't know much about it yet). Note I also changed the mysql to mysqli to make it more compatible with newer versions of PHP because mysql is no longer supported in newer versions or for the future.

Ricardo
  • 64
  • 4
  • I only made it roughly to try and explain, for instance the data sended needs to be selected; var data_to_send = document.getElementByClassName('data_to_send').value; and remember to load the DOM with $(document).ready(function() { JQuery|JavaScript code }); - if you don't actually know what's happening in the code I specified its better to read up on this before trying it. $.ajax - http://api.jquery.com/jquery.ajax/ – Ricardo Sep 01 '15 at 19:01