AleMal
AleMal

Reputation: 2087

javascript check if $_POST exist

I have this scenario:

My question is: How is possible in javascript check if a $_POST call was made??

My situation now:

  1. In my index.html:

    <form id="form1" name="form1" method="post" action="middlescan.html">
    <p>
    <label for="oStxt"></label>
    </p>
    <table width="60%" border="0" cellspacing="0" cellpadding="0">
    <tr>
    <td width="6%">&nbsp;</td>
    <td width="17%"><input type="text" name="oStxt" id="oStxt" /></td>
    <td width="77%"><input type="submit" name="button" id="button" value="Invia" /></td>
    </tr>
    </table>
    </form>
    
  2. In middlescan.html

    <body onload = ckpost()>
    

3.in js file

function ckpost() {

// only continue if we have a valid xmlHttp object
if (xmlHttp)
{
// try to connect to the server
try
{
  // initiate reading the async.txt file from the serve

  xmlHttp.open(“POST", "php/check.php", true);
  xmlHttp.onreadystatechange = handleRequestStateChange;
  xmlHttp.send(null);
  // change cursor to "busy" hourglass icon


}
// display the error in case of failure
catch (e)
{
  alert("Can't connect to server:\n" + e.toString());
}
}
}
  1. in my checkpost.php

    <?php
    $datat = array();
    
    if(!is_null($_POST['oStxt']) || $_POST['oStxt'] != "") {
    

    $datat[urlpost] = $_POST['oStxt'];

    } else {
    

    $datat[urlpost] = "nullo";

    } 
    

    //Creo il file json echo json_encode($datat);

    ?>
    

but response is ever "nullo" What is wrong?

Thanks in advance

Upvotes: 0

Views: 11213

Answers (3)

Aditya
Aditya

Reputation: 4463

$_POST is server-side variable not accessible to javascript!

If you want to check variables to be POSTed in the form before sending:

<form action="some_url" onSubmit="if (this.input_name.value == ''|| this.input_name.value==' ') { alert('empty value found'); return false;}">

If you want to detect if Ajax call was made:

 $(document).bind("ajaxSend", function(){
       alert('ajax was used!');
    });

Upvotes: 1

Patrick Evans
Patrick Evans

Reputation: 42736

You would have to do the check in php or use some ajax call to some php script as javascript has no access to the backend scripting languages except through ajax calls.

you could do the following in php if you need specific js code to be execute based on some php variable

<?php
  if(isset($_POST['somevar']) && $_POST['somevar']=="Some Value") {
     echo <<<END
     <script>
        $(document).ready(function(){
           //do something here.
        });
     </script>
END;
  }
?>

OR have a js file loaded

<?php
  if(isset($_POST['somevar']) && $_POST['somevar']=="Some Value") {
     echo <<<END
     <script type="text/javascript" src="/someJSFile.js"></script>
END;
  }
?>

But note the javascript will not run till the browser has loaded the page and is not executing within php.

Upvotes: 2

hsuk
hsuk

Reputation: 6860

Instead you can check every form elements if they are set or not on client side itself if it is about just to check if form elements are filled or empty.

Like:

if(document.getElementById("form_element1")) {
     alert('Value set');
}

Else, you got to create a bridge file in PHP, so you could call the that file via AJAX.

Upvotes: 0

Related Questions