0

I'm trying to implement a button in a moodle PHP file. Specifically, it is the SCORM window. I want the button to have the "return to the course" function. Currently, I managed to do it through a link, but I can not implement the button. I tried using HTML and calling the PHP functions within this file. I currently have the following:

    <input type="submit" name="" value="Buscar" id="boton1" onclick = "funcion();">
<script>
  function funcion(){
    alert('<?php echo accion(); ?>');
  }
</script>

function accion(){
    echo html_writer::start_div('', array('id' => 'toctree'));
    $exiturl2 = course_get_url($course, $cm->sectionnum);
    $prueba = html_writer::link($exiturl2, $strexit2, array('title' => $strexit2));
echo "<div align='center'>$prueba</div><br>"; 
$PAGE->set_button($prueba);
}
HamzaNig
  • 1,019
  • 1
  • 10
  • 33
  • 2
    don't mix php and javascript - it's really bad practice and should be avoided at all costs. – treyBake Jun 26 '18 at 11:57
  • "bad practice"?? - well, 'best', perhaps... I feel that, if the project needs doing, use whatever method you best get your head around (and sometimes that is mixing PHP and JS). "should be avoided at all cost"???? Couldn't disagree with that more (as well as any such "all or nothing" statements when it comes to programming and getting the job done). – Apps-n-Add-Ons Jun 26 '18 at 12:31
  • _“Currently, I managed to do it through a link”_ - good, then keep that, and _format it_ to look as “button-y” as you need it. Replacing it with a button would be nonsense from a semantic standpoint anyway. – CBroe Jun 26 '18 at 12:33
  • @CFPSupport no... never mix.. it's not a case of getting the job done, this is what leads to terribly coded sites and I can gurantee your projects fall into that category. They're not called "best" practices for nothing, "best" practices mean most efficient, best performance and ultimate readability – treyBake Jun 26 '18 at 12:37

2 Answers2

1

To directly address the issue - your problem is that you are

using HTML and calling the PHP functions within this file.

That doesn't work (as you can see....)

The reason is simple - and if you keep this in mind, you can use PHP in HTML files (and with great success as pages load faster when done properly as discussed here, here and all over SO when talking about various frameworks like Zend, etc....)

How to properly use PHP in an HTML file

  1. Be very careful that you are using PHP information as a 'template' and/or pulling data as the page loads. This is the mistake in the original question - you can't get data from PHP after the page loads (well, you can, just not directly - see below).

    See the links referenced for examples of how to properly do this. As the page is loading, you grab data from PHP variables and place them into input values (or whatever you need). One thing to watch out for here is running long PHP functions that gather lots of data, etc. A rule of thumb is to watch out for page 'time to display' and not use lots of functions, though variables that are set on page initialization (like gathering a user's permissions before allowing them to see the page, etc.) are quite acceptable (this is more advanced than your question, though certainly how frameworks and such are built).

2) For data that takes a 'long' time to load (like those functions mentioned above), as well as EVERY time you need to do what you are trying to do....

using HTML and calling the PHP functions within this file.

You MUST use JS and AJAX to call the PHP page and gather the data. There are tons of questions about using AJAX on SO and all over the web - read and learn about that - you will use it a LOT on making pages where you need data and/or action from PHP pages.

3) Get it in your head about 'mixing JS/PHP/HTML in one file'. What you MUST do (if you are going to ever mix them - I believe the above statements clarify when/how that is done - including in major frameworks, etc...) is what I call "jump in/out of HTML/PHP". The links to the other SO questions show this, though here is an example broken down:

Start the page with PHP Here you might need to authorize the user, etc. (one example would be to look at the PHP session and validate against a server - this gets too complex for this answer so I will keep it short)

<?php
    // gather authorization and other "can't view the page without it" variables here.  For our example, we will say that you have the $course variable
    $course = "mycourse";

     // 'jump out' of PHP and 'into' HTML by closing the PHP 
     ?> 

Now you are in HTML You can so something like set an input to the 'course' so when the page is displayed you see that in the input box (this is a very proper and common use of HTML/PHP on a page - regardless of if JS is on there or not...)

<input type="text" id="thecourse" name="thecourse" value="

we need the variable from PHP so again we 'jump' and go to PHP, echo the variable and then back out to HTML

<?php echo $course; ?>

and finish the HTML

">

Well, that is a long explanation of those examples referenced, though I trust it will help!

Another way to do it (using JS and PHP on the same page) is with something like

<script>
    // Note here that we 'jump' out of HTML and into JS!
    // also note that I'm using jQuery here as it is commonly used today
    $("#thecourse").val('<?php echo $mycourse;?>'); // again a 'jump' to/from PHP - within JS!
    // now, 'jump out' of JS and back to HTML...
</script>

There is a lot to learn about getting data moved around and formatted (and all the language differences on how to do that). Over time you will learn, though, for now, get your head around the 'jump' idea and you can do a lot of interesting things - with fast page loads showing data instantly!

Apps-n-Add-Ons
  • 2,026
  • 1
  • 17
  • 28
-1

Not exactly sure what you're after.

But maybe something like this: wrap the <input type='submit'> in a <form></form> and make the form's action point to the PHP file. In the PHP file you can then take care of redirection using header('Location: where/to/go') or whatever.

<-- html stuff -->
<form action='path/to/php-file.php'>
    <input type='submit' name="" value="Buscar" id="boton1">
</form>

One thing is for sure though, don't mix PHP and JS.

Millard
  • 436
  • 5
  • 11