NULL value sent because of first time PHP page loading

I am facing one problem because of the structure of the javascript files and PHP files I believe.

Let’s say my files names are :

  1. request.js

  2. process.php

3)submit.php

When my application loads for the first time, there is a Ajax call to the process.php file and the URL is something like this

http://<path to process.php folder>process.php?widget=AdditionalFile

So basically when my application loads for the first time, all three files are loaded

I have an HTML form defined inside submit.php file which is basically like the following with a submit button :

<form id = "myform" action="" method="post" >
    First name: <input type="text" name="FirstName" id="projectTitle" value="Mickey"><br>
    Last name: <input type="text" name="LastName" id="LastName" value="Mouse"><br>
	<button type = "button" onclick="submit()">Submit</button>
    </form>

When a user clicks on Submit button, the following function defined inside request.js is called:

function submit(){  

            console.log("Test");
			var formdata = $("#myform").serializeArray();
			var request = $.ajax({
				url: 'submit.php',
				type:'POST',
				data: myData:formdata,
				success: function(msg) {  
   					 alert(msg);
				}
			});
				
	}

I have defined a CURL based POST webservice call inside submit.php file and I am sending $_POST["myData"]; as my data with my post webservice.

The problem I am facing is that when my application loads for the first time, all the three pages are loaded including submit.php. And during this time, my curl based webservice gets called with NULL data because there’s nothing inside $_POST["myData"]; unless user clicks on the submit button. Because of this my webservice in submit.php page is throwing error. When I run the webservice with some hardcoded data in place of $_POST["myData"]; it runs fine after first time page gets loaded.

How do I prevent $_POST["myData"]; from getting submitted as NULL in the submit.php page when the application loads for the first time?

Thanks in advance !

You answered your own question, sort of.

Wrap all of thr code that needs there to be POST inside a conditional if() that tests for POST.

Thanks. That worked.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.