Post data to a PHP script

Hello,

I wish to post some data to a PHP script using an inputText and a Submit button. I have created an inputText box and a Submit button in my wix editor, and have written the below code:

import {fetch} from ‘wix-fetch’ ;

export function button3_click(event) {
console.log( "value: " + $w( “#input4” ).value) //to check the value sent

fetch( "http://www. mydomain .com/test.php" , {
“method” : “post” ,
“headers” : {
“Content-Type” : “application/x-www-form-urlencoded”
},
“body” : “mobileno:” + $w( “#input4” ).value
} )
.then( (httpResponse) => {
if (httpResponse.ok) {
console.log( “success” )
return httpResponse.json();
} else {
return Promise.reject( “Fetch did not succeed” );
}
} )
.then( (json) => console.log(json.someKey) )
. catch (err => console.log(err));
}

On button click, I am always getting ‘TypeError: Failed to fetch’ error. The PHP script runs fine if I put it in the browser and echos a string.

How do we send a simple text to a PHP file?
Any leads would help,

Thank you.

Fetch is a promise, so you don’t need this code:
return Promise.reject( “Fetch did not succeed” );

The catch error is fired on any error in the fetch and its chains.

Debug the fetch using the Dev Tools → network.
Post errors could be CORS and browser pre-flight.

I had taken the code from https://www.wix.com/velo/reference/wix-fetch/fetch under Post data to a resource.

Even after removal of the line, the error still persists. The error comes immediately, it doesn’t even try to contact the endpoint (php script). Probably some error in how the fetch is written?

@msureka Did youd debug using Chrome Dev Tools (f12)?