0

I have a main phtml script and I want to be able to run another phtml script when a user clicks the submit button within the main script.(This is all being done using php and html) Currently this is how I'm doing it.

<input id="submit" type="submit" name="submit" value="Submit" onclick="self.location.href='another.phtml'"/> 

When I run this, it will load the page but it won't actually execute anything in the script. And I do know it works because I am able to execute it successfully when calling it using:

<a href="another.phtml">Click Here</a> 

I've also tried creating an additional php script, and using include to include the additional phtml file, and then call it with the same method above.

I've also tried to use javascript (See example: How do I run PHP code when a user clicks on a link?)

Is there another way I can do this?

Thank you for your time

Community
  • 1
  • 1
user1186173
  • 565
  • 3
  • 7
  • 26
  • There should be no differnce between submitting a form to a url, or hitting the url via a click - they're both just HTTP requests. If it works for one, it'll work for the other. – Marc B Jul 20 '12 at 16:02
  • 1
    I'm thinking this is to do with the fact that you have ``. Try using an `` instead. – DaveRandom Jul 20 '12 at 16:03
  • I tried both suggestions, and it still doesn't work. When submitting it like a url, it won't load the page or do anything. When submitting it as a "button", it loads the page but doesn't execute the script. – user1186173 Jul 20 '12 at 16:10
  • As an additional thought, is there any point in having a click handler and redirecting with javascript? If it's a submit input, why not just use the form's action attribute to point it in the right direction? – Brian Warshaw Jul 20 '12 at 18:31
  • That is an alternative way, I did try though and it didn't work at the time. My problem was I needed to create an action in my controller to allow the phtml to work properly. So it would probably work now if I try that. – user1186173 Jul 20 '12 at 19:33

1 Answers1

0

Create an action and use this (another.phtml) view in that action, you can set view file inside the action using View Renderer action helper. Making request (or, in your words, running the script) on clicking the link is done using Ajax. It's covered extremely widely on the internet so I won't get into the details.

In Zend Framework, phtml files are view files: they are supposed just to print variable values, print HTML tags and etc. And the amount of PHP code in them is very minimal: loops and etc, just what you need to print something. And making DB requests or any other kind of business logic inside them is considered to be bad design, bad separation of logic. They are not meant to be ran outside controller, stand alone.

Hope it helped, let me know if I misunderstood something.

Gediminas
  • 854
  • 8
  • 14
  • Right before I saw this, I figured out how I need to create an action in the controller, and it worked. Thank you for the additional information though. All I really want to do is create a file based on some information that I can get from the controller, I don't need it going to another page. I would actually prefer that it would stay on the same page. However, I'm not sure how exactly to do it using onclick() in the main phtml file. – user1186173 Jul 20 '12 at 18:55
  • @user1186173 what do you mean by 'create a file'? You need to make an AJAX request, it's done using JavaScript library of your choice (usually jQuery). There is nothing Zend specific in it, and there is ton of tutorials on the net. One of them: http://net.tutsplus.com/tutorials/javascript-ajax/5-ways-to-make-ajax-calls-with-jquery/ – Gediminas Jul 20 '12 at 19:40
  • Its a file based on information that I get from the controller, which is why I need the controller. And I've never used AJAX and not very much of javascript – user1186173 Jul 20 '12 at 19:42