0

i need a little help with my php code

here's my whole code

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

what i am trying to do here is , when i clicked o submit button it must show me YES and NO options. if i clicked on YES then it must excute the code and if i clicked NO then do nothing. hope you get it

Mm Pp
  • 227
  • 3
  • 9

3 Answers3

0

Try this I have used javascipt confirm function to add a yes or no. And document.forms[0].submit() for submission of your first form.

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> 
Resheil Agarwal
  • 155
  • 1
  • 11
  • 1
    Is this copied from here http://stackoverflow.com/questions/6515502/javascript-form-submit-confirm-or-cancel-submission-dialog-box#answer-6515566??? – Ionut Necula Mar 05 '17 at 00:18
  • If you're gonna copy/paste someone else answer, you should at least give them credit and link to it... – M. Eriksson Mar 05 '17 at 00:22
0

So you need to register an event listener to the form to listen for the submit button click.

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.
    });

Do not forget to pass the ID you specified in document.getElementById to your form: <form class="s_submit" method="post" id="submitForm"></form>

You can read more about the preventDefault() function here: https://www.w3schools.com/jsref/event_preventdefault.asp

Larce
  • 841
  • 8
  • 17
0

A good way to achieve this is jquery. I made you an example of how to go about this.

$('#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>
  
Yolo
  • 1,569
  • 1
  • 11
  • 16