2017-04-20 70 views
-2

你好,我有一個json格式的問題。將textarea轉換爲json /和其他float值/

這裏是代碼

If (isset($_POST['submit']) && $_POST['submit']='submit') 
{ 
$data['value1']=(float)$_POST['val1']; //values from input fields 

... 
... 
... 
$data['value6']=(float)$_POST['val6']; 
$data['textar']=$_POST ['textarea'];//from textarea 
$data['textar2']=$_POST ['textarea2']; //from textarea 

json_encode($data,JSON_PRETTY_PRINT)); 
} 

postR($data); 

function postR($data) 
{ 
$curl = curl_init(); 
$url = COC_HOST."xxx"; 

echo $url; 


curl_setopt_array($curl, array(
    CURLOPT_URL => $url, 
    CURLOPT_RETURNTRANSFER => true, 
    CURLOPT_SSL_VERIFYPEER => false, //temporary, to ignore ssl cert errors 
    CURLOPT_ENCODING => "", 
    CURLOPT_MAXREDIRS => 10, 
    CURLOPT_TIMEOUT => 300, 
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, 
    CURLOPT_CUSTOMREQUEST => "POST", 
    CURLOPT_POSTFIELDS => json_encode($data), 
    CURLOPT_HTTPHEADER => array(
     "authorization: ".COC_HEADER_AUTHENTICATION, 
     "cache-control: no-cache", 
     "content-type: application/json" 
    ), 
)); 

$response = curl_exec($curl); 
$err = curl_error($curl); 

curl_close($curl); 

if ($err) { 
    echo "cURL Error #:" . $err; 
    return false; 
} else { 

    return $response; 

} 

}

HTML

<form method="post" action=""> 

Value1:<input type="text" size="5" value="" name="val1"> 
Value2:<input type="text" size="5" value="" name="val2"> 
Value3:<input type="text" size="5" value="" name="val3"> 
Value4:<input type="text" size="5" value="" name="val4"> 
Value5:<input type="text" size="5" value="" name="val5"> 
Value6:<input type="text" size="5" value="" name="val6"> 

IDs :<input type="textarea" value="" name="textarea"> 
Notes:<input type="textarea" value="" name="textarea2"> 
<input type="submit" name="submit" value="submit"> 
</form> 

但是,這將返回我,當然還有字符串錯誤。 Textar和textar2是textareas,而其餘數據是浮動的。如何將此textarea轉換爲有效的json? Value1到value6必須像貨幣字段一樣浮動。

以上代碼由手寫入,因爲我沒有訪問計算機的權限。

我試着插入nulls到textarea的值,但它不起作用,這個字段將來不會變空。

謝謝。

+0

什麼是你輸入的textareas? – MackProgramsAlot

+0

一些文字 – widmopl

+0

請在表單中加入代碼。 – julekgwa

回答

0

除了額外的),在json_encode($data,JSON_PRETTY_PRINT));代碼看起來沒問題這段代碼沒有什麼錯,除非你正在做別的事情。

<html lang="en"> 
<head> 
    <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> 
</head> 
<body> 
<form id="test" method="post" action=""> 

    Value1:<input type="text" size="5" value="" name="val1"> 
    Value2:<input type="text" size="5" value="" name="val2"> 
    Value3:<input type="text" size="5" value="" name="val3"> 
    Value4:<input type="text" size="5" value="" name="val4"> 
    Value5:<input type="text" size="5" value="" name="val5"> 
    Value6:<input type="text" size="5" value="" name="val6"> 

    IDs :<input type="textarea" value="" name="textarea"> 
    Notes:<input type="textarea" value="" name="textarea2"> 
    <input type="submit" id="sub" name="submit" value="submit"> 
</form> 
<script> 
    $('#sub').on('click', function (e) { 
     e.preventDefault(); 
     var data = $('#test').serialize(); 
     $.post('check.php', data, function (res) { 
      console.log(res); 
     }) 
    }) 
</script> 
</body> 
</html> 
在PHP

<?php 

//if (isset($_POST['submit']) && $_POST['submit']='submit') { // commented out submit buttons are not sent with ajax 
    $data['value1'] = (float)$_POST['val1']; //values from input fields 
    $data['value2'] = (float)$_POST['val2']; //values from input fields 
    $data['value3'] = (float)$_POST['val3']; //values from input fields 
    $data['value4'] = (float)$_POST['val4']; //values from input fields 
    $data['value5'] = (float)$_POST['val5']; //values from input fields 
    $data['value6'] = (float)$_POST['val6']; 
    $data['textar'] = $_POST ['textarea'];//from textarea 
    $data['textar2'] = $_POST ['textarea2']; //from textarea 
//use echo to display the result of json_encode 
    echo json_encode($data, JSON_PRETTY_PRINT); 
//} 

控制檯日誌

results

+0

感謝您的回覆 – widmopl

+0

@widmopl這段代碼的用途是什麼? – julekgwa

+0

剛剛添加到主窗口上面的一些代碼,它會改變什麼嗎? – widmopl