-3

I am quite certain this is an easy thing for someone to answer but since I am still pretty new to webpage design I figured I would ask it anyway. All I am trying to do is create a button to which when clicked will load another page (either .html or .php). I am doing all of this within an existing PHP page so of course it needs to be echoed.

Currently what I have tried is this:

echo '<input type="button" onclick="save_playlist.php" value="Save Playlist">';

Am I missing something very simple? I am not passing any data, simply trying to load the save_playlist.php.

Thanks in advance.

Eric F
  • 899
  • 2
  • 21
  • 45

2 Answers2

0

Despite the downvotes, I can see that you are genuinely confused on what to do and, quite frankly, it's hard to ask the right question when you aren't sure what is going on. To navigate between files in the client side, you have a few options.

  • Anchor Tags - This is the easiest way and has the best cross-browser support.

    <a href="save_playlist.php">Save Playlist</a>

  • Button - The <button> element provides behavior like the <input element with the added ability to include HTML within the tags. Unfortunately, it's not supported in all browsers, but then again, the ones that don't need to die off anyway.

  • Javascript - This seems to be the option you are trying to figure out. What happens when you click a button is it propogates a click event which you can listen to and run code when it occurs. So, if we give your <input> element an ID, we can then set a listener to perform an action when the event occurs:

    <html>
    
      <body>
    
         <input type="button" id="savePlaylist" value="Save Playlist">
    
         <script>
    
            document.getElementById('savePlaylist').onclick = function(e){
              // Goto another page
              window.location.replace('save_playlist.php');
            }
    
          </script>
    
      </body>
    
    </html>
    

Looking at the name of the file you are trying to redirect to, it more so sounds like you are trying to submit data to be saved. In order to trigger a POST request, you have a couple of options:

  • Use a <form> tag and make your input button be of type='submit'. This submission event can be intercepted with javascript if you choose to for validation or whatever you need to do.

  • Use AJAX to submit data to another script asynchronously. Given your level of experience, I would say learn to use forms properly first before moving into AJAX.

Community
  • 1
  • 1
Jeremy Harris
  • 24,318
  • 13
  • 79
  • 133
  • Thank you so much for the effort. I felt silly asking the question but I was definitely confused being why I asked and all other similar posts on SO weren't quite what I was asking. Again thank you. – Eric F May 28 '14 at 01:33
0

Thanks to aldanux above. Here is how I solved my problem:

echo '<input type="button" value="Save Playlist" onClick="window.location.href=\'http://example.com/save_playlist.php\'">';
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Eric F
  • 899
  • 2
  • 21
  • 45