-2

I am new in PHP. I have a form which have different submit button Like Save, Edit, Cancel and Save as. Now i want when user click on Save as button java script is run and Check the file name. If there is a already exists file with same name in db then display a alert message "Please Change file name" if user change file name then execute the code of button. I use code for buttons

<input type="submit" name="submit" value="Save as" onclick="show_confirm()" />
<input type="submit" name="submit" value="Make a copy" />

and in PhP

if($_POST['submit'] == 'Make a copy') {
        $action = "copy";
    } elseif($_POST['submit'] == 'Save as') {
        $action = "save as";
    }

and may be java script like this

<script type="text/javascript">
    function show_confirm()
        {
            var r=confirm("Please Change File Name!?");
            if (r==true)
            {                   
            }
            else
            {                    
            }
        } 
</script>

my form look like this save as

Now i am trying to write my Java Script for alert message can u please help me to sort out my problem?

sunny
  • 1,511
  • 5
  • 20
  • 57
  • Do not do it this way, instead, when user chooses a file for upload, send an AJAX request to server and check if there is already a file then ask for use to perform diff action – Umair Ayub Jul 02 '15 at 06:19
  • we can't use same submit button name twice :) – Elangovan Jul 02 '15 at 06:20
  • @Elangovan oh sure we can – Mike Jul 02 '15 at 06:21
  • @Umair there is no file upload option on my form – sunny Jul 02 '15 at 06:21
  • there is no file upload option on my form ` so what are your trying to upload/save or replace? :P – Umair Ayub Jul 02 '15 at 06:21
  • @Elangovan we can do it i just show in my PHP code how we use SUBMIT Twice – sunny Jul 02 '15 at 06:22
  • @ Umair i am trying that when user click on Edit button then it is able to save same data with different file name – sunny Jul 02 '15 at 06:22
  • @Umair i Update my question with Image of my form – sunny Jul 02 '15 at 06:26
  • Dont name the buttons the same btw, php won't like that when you check for a submit. It won't know which one you pressed. And start with an `if(isset($_POST['submit'])){` and then your submit statement, else you make php code run unneccesairy – Dorvalla Jul 02 '15 at 06:27
  • If I understand your question correctly, you want some AJAX to run when you click "Save as" to poll the server for an existing file name. If such a file name exists, then display an alert message. Is this correct? – Drakes Jul 02 '15 at 06:27
  • @Drakes yes i exactly want this – sunny Jul 02 '15 at 06:28
  • May I ask, what name should it check for, How do you determine the name to seek? Or can only be 1 file uploaded at the time? – Dorvalla Jul 02 '15 at 06:30
  • @Dorvalla look at image of my form. There is no file upload option. It is a form which store data in database and after that i fetch that data in PDF format. And here file name is actually the name of file of PDF – sunny Jul 02 '15 at 06:32
  • Yes, I get that sunny, but how do you determine what information it should be picked up. You check by id, or by name, that was what i meant? – Dorvalla Jul 02 '15 at 07:31
  • @Dorvalla i have done my work with some other solution – sunny Jul 02 '15 at 07:33

2 Answers2

1

instead of submit button with same name, use these

<input type="button"  value="Save as" onclick="show_confirm(1)" />
<input type="button" value="Make a copy"  onclick="show_confirm(2)" />
<input type="hidden" value="" id="what_to_do" name="what_to_do" />
enter code here

and in your script

<script type="text/javascript">
    function show_confirm(a)
    {

        if (a == 1) {

            $("#what_to_do").val("save_as");
        }
        if (a == 2) {

            $("#what_to_do").val("make_copy");
        }

        // now submit form
        $('#myForm').submit();
    }
</script>

And in you PHP side

<?php

 // $_POST['what_to_do'] is actually this one -  <input type="hidden" value="" id="what_to_do" name="what_to_do" />
if ($_POST['what_to_do'] == 'make_copy') {
    $action = "copy";
} elseif ($_POST['what_to_do'] == 'save_as') {
    $action = "edit";
}
Umair Ayub
  • 19,358
  • 14
  • 72
  • 146
1
<form action="program.php" method="post">
    Default value in this case: Maurize
    <input type="text" name="filename" placeholder="Username"/><br>
    <input type="submit" name="submit" value="Save as"/>
    <input type="submit" name="submit" value="Make a copy" />
</form>

now you can check names:

<?php
$oldUsername = "Maurize"; //Will be triggered out of database

$newUsername = $_POST["filename"]; //This is from html
switch ($_POST['submit']) {
    case 'Save as':
        if ($oldUsername == $newUsername){ //If the name is already present
            echo('<script>alert("Please change your filename because it already exists"); window.location.href = "index.html";</script>');
        }
        else{
            echo('<script>alert("Creating your filename was succesfully"); window.location.href = "index.html";</script>');
        }
    break;
    case 'Make a copy':
        echo "make a copy";
    break;
}
?>

Now we have our standart form at top. If we submit this our program.php gets called like below. If the name is already present we alert him and send him back to page.

  • Maurize i want something different. I want to display alert message on save as button after checking file name – sunny Jul 02 '15 at 06:39
  • You have to check filename in php with your value in database first. If this check gives out true for example you can trigger the javascript. Give me a moment. –  Jul 02 '15 at 06:40
  • try this. was the fastest solution I got. –  Jul 02 '15 at 06:53
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/82164/discussion-between-sunny-and-maurize). – sunny Jul 02 '15 at 06:59