2014-09-27 57 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