简体   繁体   中英

Save data in database in php

I have built two pop up divs in my webpage. Initially the first div - div1 - is loaded which contains drop down menus. This div1 contains a button Next which will close the first div and open the new div; div2 . Div2 contains input fields where the user will enter the name, email address, phone number etc and click on the button submit . The submit button call on the php code that connects to the sql database and saves the data in div2 to the database:

/* Specify the server and connection string attributes. */
$serverName = "xxx-PC\SQLExpress";
$connectionOptions = array("Database"=>"Salesforce");
$conn = sqlsrv_connect($serverName, $connectionOptions);
if($conn === false)
{
      die(print_r(sqlsrv_errors(), true));
}
else 
{
/*Insert data.*/
$insertSql = "Insert into Salesforce.dbo.customer_details (Name, Company, Email, Phone, Assets, Comm, Capability)
VALUES (?,?,?,?,?,?,?)";
* Construct the parameter array. */
$params1 = array($from_fullname ,
               $email_message,
               $from_email,
               $Phone,
               'vehicles',
               'xyz',
               'abc');
$stmt = sqlsrv_query($conn, $insertSql, $params1);
if($stmt === false)
{
/*Die if other errors occurred.*/
die('Errors: ' . print_r(sqlsrv_errors(), TRUE));
}
else
{
echo '<br clear="all"><label style="color:red;">Thank you for your details.</label>';
}

I tried to get the data from the drop down in div1 through the following code but it didnt work:

$assets = trim(strip_tags($_POST['track']));

where track is:

<select id="track" name="track" style="background-color: #ffffff;" onchange="selection(this)">
        <option value="personnel">Personnel</option>
        <option value="vehicles">Vehicles</option>
        <option value="personnelVehicles">Personnel & Vehicles</option>
        <option value="maritime">Maritime</option>
        <option value="aircraft">Aircraft</option>
        <option value="all">All of the above</option>
</select>

How can I fetch data from the dropdowns from div1 and pass it to the php script when submit is clicked from div2 ?

EDIT1: The submit button calls on the javascript where div2 fields are checked. In here, I can pick the value of the dropdown of div1 through the following:

var assets = $("#track :selected").text(); //the text content of the selected option

Can I pass this variable from the javascript to the php file where I need to make call to the SQL connection?

You can use sessions to store the data and retrieve it as and when needed

For Ex:

if(isset($_POST['div2']))
{
//use session variables
$div1_data=$_SESSION['xyx'];
//And then can insert using prepared statements
}

NOTE: Since you are saying that these two divs are not in a form, you can use a jquery AJAX method to send the datas asynchronously to tht php page which performs the insert option

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM