在显示代码之前,php显示警告

时间:2017-03-05 00:07:20

标签: javascript php jquery html

我的php代码需要一些帮助

这是我的整个代码

<!doctype html>
<html>
<head>
   <meta charset="utf-8">
   <title>Untitled Document</title>
</head>

<body>

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST")
{ 
    if(empty($_POST['filename']))
    {
       $schanName[] = 'File Name is Required';
    }

    if($_POST['thisfolder'] == 'default') 
    { 
       $schanName[] = 'Please select a Folder'; 
    }

    $filename=$_POST['filename']; 
    $words = array("1", "2", "3", "4", "5"); 
    $arrlength = count($words); 
    $found = false; 

    for($x = 0; $x < $arrlength; $x++)
    { 
       if($filename == $words[$x]) 
       { 
          $found = true; 
       } 
    } 

    if($found) 
    { 
       $schanName[] = 'Not a valid File Name';
    } 

    // the name of the file to create 
    $filename=$_POST['filename']; 
    // the name of the file to be in page created 
    $strin=$_POST['strin']; 
    // the name of the file to be in page created 
    $strin2=$_POST['strin2']; 
    // the name of the folder to put $filename in 
    $thisFolder = $_POST['thisfolder']; 
    // make sure #thisFolder of actually a folder 
    if (!is_dir(__DIR__.'/'.$thisFolder))
    { 
        // if not, we need to make a new folder 
        mkdir(__DIR__.'/'.$thisFolder); 
    } 
    // . . . /[folder name]/page[file name].php 
    $myFile = __DIR__.'/'.$thisFolder. "/page" .$filename.".php"; 

    // This is another way of writing an if statment 
    $div = ($strin !== '') ? '<div id="area_code">'.$strin.'</div>' : '<div   id="area_code">'.$strin2.'</div>'; 

   $fh = fopen($myFile, 'w'); 
   $stringData = ""; 

   fwrite($fh, $stringData); 
   fclose($fh); 
} 

?>


<?php
  // display your errors here
  if(!empty($schanName))
  {
     foreach ($schanName as $sn)
     {
        echo '<div id="error"><ul><li>'.$sn.'</li></ul></div>';
     }
  }
?>  


<form class="s_submit" method="post"> 
<label class="def_lab">File:</label> 
<input class="t_box" type='text' name='filename' placeholder='File Name'> 
<label class="t_lab def_lab">Select Folder:</label> 
<select id="soflow" name="thisfolder"> 
    <option selected="selected" value="default">Default</option> 
    <option value="../embed/tv/xbox/">Xbox</option> 
    <option value="Folder2">Folder2</option> 
    <option value="Folder3">Folder3</option>
</select><br><br> 
<label class="def_lab">Text Area 1:</label><br> 
<textarea class="tarea_box" type='text' name='strin'></textarea><br><br> 
<label class="def_lab">Text Area 2:</label><br> 
<textarea class="tarea_box" type='text' name='strin2'></textarea><br> 
<button type="submit" class="btn btn-primary">Submit</button> 
</form> 

</body>
</html>

我想在这里做的是,当我点击提交按钮时,它必须显示YES和NO选项。如果我点击YES然后它必须执行代码,如果我点击否则不执行任何操作。希望你明白

3 个答案:

答案 0 :(得分:0)

试试这个我用javascipt confirm函数添加yes或no。并且document.forms [0] .submit()用于提交您的第一个表单。

function show_alert() {
      if(confirm("Do you really want to do this?"))
        document.forms[0].submit();
      else
        return false;
    }

  <button type="submit" class="btn btn-primary" onclick="show_alert()">Submit</button> 

答案 1 :(得分:0)

因此,您需要向表单注册一个事件监听器,以侦听提交按钮单击。

document.getElementById('submitForm').addEventListener("submit", function(event) {
        // Show the confirm dialog
        // if no is clicked
        if (!confirm("Are you sure?")) {
            // Do not submit the form
            event.preventDefault()
        }
        // if we do not explicitly say to prevent the default action, the form will be submitted.
    });

不要忘记将您在document.getElementById中指定的ID传递给表单:<form class="s_submit" method="post" id="submitForm"></form>

您可以在此处详细了解preventDefault()函数:https://www.w3schools.com/jsref/event_preventdefault.asp

答案 2 :(得分:0)

实现这一目标的一个好方法是jquery。我告诉你如何解决这个问题。

$('#dialog').dialog({
      modal: true,
      autoOpen: false,
        closeOnEscape: true,
        buttons : {
            "Confirm" : function() {
              alert('Success callback called');
              $('form.s_submit').submit(); // submit form
              $(this).dialog("close");
            },
            "Cancel" : function() {
              $(this).dialog("close");
            }
          }
    });
    
$("#someButton").click(function(e) {
    e.preventDefault();
    $('#dialog').dialog('open');
});
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.0/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js"></script>
  <div id="dialog"><p>Hello dialog!</p></div>
  
  <button id="someButton" type="submit" class="btn btn-primary">Submit</button>