2014-09-27 50 views
0

我是JavaScript新手,我很難在腳本中解決一個問題。 我想通過變量「outVal」,當我點擊提交表單到PHP腳本。 「outVal」的值將從腳本中的「output」的值中獲得。 我不知道如何將「輸出」的值傳遞給「outVal」。 我已經搜索瞭解決方案,並嘗試了它的每一個,但它不起作用。 也許有人可以幫我找到一個適當的解決方案。 我的代碼如下: 注意:兩個代碼都在同一個文件中。以形式傳遞Javascript變量

HTML:

<{includeq file="db:frontend_breadcrumbs.tpl"}> 
<div> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<script src="./assets/js/jsoneditor.min.js"></script> 

<div class='row'> 
    <div class='span8 col-md-8 columns eight large-8'> 
     <form action="./applyProcess.php" method="post" enctype="multipart/form-data"> 
      <label for="file">Filename:</label> 
      <input type="file" name="file" id="file"><br> 
      <input type="submit" name="submit" value="Upload"> 
      <input type="hidden" name="hashAlgo" value="<{$hashAlgo}>"> 
      <input type="hidden" name="tempDirName" value="<{$tempDirName}>"> 
      <input type="hidden" name="op" value="upload"> 
      <input type="hidden" id="outVal" name="outVal"> 
     </form> 

     <div id='editor_holder'></div> 
    </div> 
</div> 

    <textarea id='output' style='width: 100%; height: 300px; font-family: monospace;' class='form-control'></textarea> 

的Javascript:

<script> 
(function() { 
    var jsoneditor; 
    // Divs/textareas on the page 
    document.getElementById('output').style.display = 'none'; 
    var $output = document.getElementById('output'); 
    var $editor = document.getElementById('editor_holder'); 
    var $outVal = document.getElementById('outVal'); 


    var $jsonschema = <{$applyProcessJsonSchema}>; 

    var reload = function(keep_value) { 
     var $startvalue = (jsoneditor && keep_value)? jsoneditor.getValue() : <{$applyProcessRequestData}>; 
     if(jsoneditor) jsoneditor.destroy(); 
     jsoneditor = new JSONEditor($editor,{ 
      ajax: true, 
      schema: $jsonschema, 
      startval: $startvalue, 
      disable_array_add: true, 
      disable_array_delete: false, 
      disable_array_reorder: true, 
      disable_edit_json: true, 
      disable_properties: true 
     }); 

     // When the value of the editor changes, update the JSON output and validation message 
     jsoneditor.on('change',function() { 
      var json = jsoneditor.getValue(); 
      $output.value = JSON.stringify(json,null,2); 
      var validation_errors = jsoneditor.validate(); 
      if(validation_errors.length) { 
       var error = JSON.stringify(validation_errors,null,2); 
       alert("Validation Error" + error); 
       reload(true); 
      } 
     }); 
     $outVal.value = $output.value; 
    }; 
    reload(); 
})(); 

function applySubmit() { 
    var $output = document.getElementById('output'); 
    var $outVal = document.getElementById('outVal'); 
    $outVal.value = $output.value; 
} 
</script> 
+0

在輸入元素中有'name'將使它們在表單提交時發送(注意它獲得'value')。您是否嘗試過'$ _POST [「outVal」]'只設置了一些默認的'value'? – 2014-09-27 04:18:03

+0

哦對不起,我不清楚我想做什麼。我想在javascript中獲得「輸出」的值,然後將它傳遞給「outVal」,然後我將在php文件中使用$ _POST ['outVal'],當我想要它時。我已更新我的問題。對困惑感到抱歉。 – user3916984 2014-09-27 04:23:50

+0

我試着發送「outVal」的默認值,當我使用$ _POST ['outVal']時,我可以得到這個值。 – user3916984 2014-09-27 04:47:05

回答

0

提交表單調用applysubmit函數之前,它修改表單值,請檢查下面的代碼,添加onsubmit事件形成元素,

<form action="./applyProcess.php" method="post" onsubmit="return applySubmit();" enctype="multipart/form-data"> 


function applySubmit() { 
     var $output = document.getElementById('output'); 
     var $outVal = document.getElementById('outVal'); 
     $outVal.value = $output.value; 
     return true; 
    } 
+0

我試過你的解決方案,但是當我在php文件中使用$ _POST ['outVal']時,我沒有得到「outVal」的值。 – user3916984 2014-09-27 04:47:52

+0

嘗試從您的PHP – 2014-09-27 04:50:42

+0

var_dump($ _ POST)或print_r($ _ POST)我試着var_dump($ _ POST),結果是這樣的:'array(5){[「submit」] => string(6)「Upload」 [「hashAlgo」] => string(7)「SHA-256」[「tempDirName」] => string(56)「/ var/www/html/xoops/modules/frontend/cache/5426b2587c138」[「op」]] => string(6)「upload」[「outVal」] => string(0)「」}' – user3916984 2014-09-27 04:52:52