0

I have an HTML file, and in the file, I have a javascript validate function to validate if the input of the text field in the other PHP file is blank or not. How can I use the 'gateway' name from myFile.html. This is what i have now:

myfile.html

<script type="text/javascript">
    function Validate() {
            if ((document.getElementsByName("gateway")[0].value == '')) {//I need to get access to "gateway" from myOtherFile.php
                    alert('Response required');
                    return false;
            }
            else { return true; }
    }
</script>

myOtherFile.php

<div id="gatewayInput">
      <form action="/action_page.php">
           Gateway: <input type="text" name="gateway" placeholder="Gateway Name"><br>
      </form>
</div>
0

3 Answers 3

1

@kkmoslehpour there are three ways

make an Ajax call

function Validate() {

  var ajax = new XMLHttpRequest();

  ajax.onreadystatechange = function() {
      if(ajax.readyState == 4 && ajax.status == 200) {
          var value_from_gateway = ajax.responseText;
      }
  }

  ajax.send();
  ajax.open("Get", "myOtherFile.php?value_from_gateway", true);

  if ((value_from_gateway == '')) {//You have access now to "gateway" from myOtherFile.php
                alert('Response required');
                return false;
        }
        else { return true; }
}

create a redirect function on the myOtherFile.php on the top on the page -NECCESSARY

function redirect($location) {
header("Location: ". $location);

}

then program your myOtherfile.php to work with th $_GET and send the value

<div id="gatewayInput">
  <form action="/action_page.php" method="post">
       Gateway: <input type="text" name="gateway" placeholder="Gateway Name"><br>
  </form>
</div>
<?php

  if(isset($_GET['value_from_gateway'])) {
      $gateway_value = $_POST['gateway'];
      redirect('value_page.php?value_to_js='. $gateway_value);
  }

?>

then create the whole value_page.php to echo the value on that same page;

<?php  // This is all that there should be on this page
  if(isset($_GET['value_to_js'])) {
      echo $_GET['value_to_js'];  //we are echoing this! it will be the response to the Ajax
  }
?>

The ajax call wont have any problem with this, and whatever is display on the current page (value_page.php) will be sent as the response in myfile.php

Sign up to request clarification or add additional context in comments.

4 Comments

If you include "myOtherFile.php", is it going to include the whole page? I just need to use the variables from this page.
don't forget your FORM should be submitted with a post method other than that it wont work.
in my case would value be "gateway"?
@kkmoslehpour sorry about the ambiguity, I have added your code for clarity now.
0

I cannot comment but if you want to get info from your php file by your html file and javascript it could be done with ajax call to check what is inside php file and returns back with some info.

Otherwise you need to include that html file with script below your element with that name so you can use javascript to find that element and get its what you want.

Comments

0

You should put the validation code into the same file as the form, as it has to run on the client side. But if for some reason you really have to have it in a separate file (and that file only contains the script tag):

echo file_get_contents('myfile.html');

It is now magically in this file as well. But seriously just put it in the same file.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.