簡體   English   中英

在php中將數據保存在數據庫中

[英]Save data in database in php

我在網頁中建立了兩個彈出式div。 最初,第一個div- div1被加載,其中包含下拉菜單。 這個div1包含一個Next按鈕,它將關閉第一個div並打開新的div; div2 Div2包含input字段,用戶將在其中輸入姓名,電子郵件地址,電話號碼等,然后單擊submit按鈕。 submit對連接到PHP代碼按鈕調用sql數據庫並保存數據div2到數據庫:

/* 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>';
}

我試圖通過以下代碼從div1的下拉列表中獲取數據,但沒有成功:

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

track在哪里:

<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>

當從div2單擊submit時,如何從div1的下拉列表中獲取數據並將其傳遞給php腳本?

EDIT1: submit按鈕在檢查div2字段的javascript上調用。 在這里,我可以通過以下方法選擇div1的下拉列表的值:

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

我可以將此變量從javascript傳遞到需要調用SQL連接的php文件嗎?

您可以使用會話存儲數據並在需要時檢索數據

例如:

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

注意:由於您說的是這兩個div都不是表單,因此可以使用jquery AJAX方法將數據異步發送到執行插入選項的php頁面

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM