-1

I have a form in which by using javascript and in a while I add content to it. Denpending on the number of data in database different number of forms are added to the page.

<?php
while($row = mysql_fetch_array ($result)){
?>
<form method="post" accept-charset="UTF-8" >
//add some content to the form by reading from database
<div class="form-group ">
            <input type="submit" name="print" id="button" value="print"/>
        </div>

    </form>
<?php
}//end while
?>

and below I want to redirect to another page when clicked on each "print" button. But not only it doesn't redirect to another page but also alert just works for the first button. Below is the code:

<script>
$("#button").click(function(){
    alert('hi');
    window.location.href = "design_card.php";
        });
</script>

how to solve? jquery, php or javascript solutions are acceptable. other workable solutions, if work, are good.

Thanks for your response in advance.

  • 2
    The problem is because you're creating multiple elements with the same `id` attribute. They must be unique. Change the `id` to a `class` instead – Rory McCrossan Jun 30 '17 at 14:02
  • thanks, it worked how can I open another .exe program when clicked on this button? – saeed doozandeh Jun 30 '17 at 14:29
  • You cannot open local programs through the browser in JS - it would be a massive security flaw if you could – Rory McCrossan Jun 30 '17 at 14:31
  • how about through php? for example I send the user to another page and that php page does opening the program. – saeed doozandeh Jun 30 '17 at 14:40
  • PHP runs on the server. It cannot open anything on the client. – Rory McCrossan Jun 30 '17 at 14:40
  • so I must buy a host, put the program on the host and try openning the program through host? – saeed doozandeh Jun 30 '17 at 14:42
  • No not at all - I'm saying that you cannot run an EXE on the client machine through a web browser without them explicitly installing something on their machine first to associate a file type. – Rory McCrossan Jun 30 '17 at 14:43
  • See this answer for more info: https://stackoverflow.com/a/1791748/519413 – Rory McCrossan Jun 30 '17 at 14:44
  • after installing on the user's machine, what should I do? – saeed doozandeh Jun 30 '17 at 14:46
  • Redirect the browser to a file with that extension. The browser will download it, then open the program associated with that extension. However like I said, you will need to get the user to install a standalone program on their machine first - which most people with any sense won't do unless they know who you are and trust the content you're giving them. – Rory McCrossan Jun 30 '17 at 14:47
  • just 1 user needs to work with my web application not a lot of users. so I need to install the software on one machine. after installing, how can I Redirect the browser for a user to a file with that extension? – saeed doozandeh Jun 30 '17 at 14:52
  • `window.location.assign('yourfile.yourextension')` – Rory McCrossan Jun 30 '17 at 15:23
  • Can I assign any address in the 'yourfile.yourextension' or just name of the file? If possible how should the format of the address be? – saeed doozandeh Jun 30 '17 at 16:15
  • You should really do your own research on this. The path can be anything. The important part is that the file extension matches the file association you created in the standalone app your user installed previously. – Rory McCrossan Jun 30 '17 at 16:18
  • thanks so much the last comment ==> window.location.assign('yourfile.yourextension') really helped I don't know how to show my gratitude towards you – saeed doozandeh Jun 30 '17 at 16:44

1 Answers1

0

Check your HTML

<?php
while($row = mysql_fetch_array ($result)){
?>
<form method="post" accept-charset="UTF-8" >
//add some content to the form by reading from database
<div class="form-group ">
            <input type="submit" name="print" id="button" value="print"/>
        </div>

    </form>
<?php
}//end while
?>

You have created multiple <input> element dynamically with duplicate id so the better way is to assign a class to them like

<?php
while($row = mysql_fetch_array ($result)){
?>
<form method="post" accept-charset="UTF-8" >
//add some content to the form by reading from database
<div class="form-group ">
            <input type="submit" name="print" class="buttonClass" value="print"/>
        </div>

    </form>
<?php
}//end while
?>

And then use this buttonClass class as a jquery selector like this

$(".buttonClass").click(function(){
    alert('hi');
    window.location.href = "design_card.php";
});
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62