0
我有一個帶有2個字段的HTML表單名稱和年齡,並且我創建了一個MySQL數據庫,並且我需要使用php API將數據從HTML存儲到數據庫中... 我是熟悉PHP,但我需要使用API從表單提交時從HTML頁面發送值...代碼我用於HTML和API。使用PHP的休息api將Html連接到Mysql
的Index.html
<form class="col s12" method="post" action="">
<input id="name" type="text" name="Name">
<input id="name" type="text" name="Age">
<button type="submit" name="btn_login">Request</button>
</form>
reg.php(API)
$app->post('/createstudent', function() use ($app) {
verifyRequiredParams(array('Name', 'Age'));
//Creating a response array
$response = array();
//reading post parameters
$name = $app->request->post('Name');
$age = $app->request->post('Age');
//Creating a DbOperation object
$db = new DbOperation();
//Calling the method createStudent to add student to the database
$res = $db->createStudent($name,$age);
//If the result returned is 0 means success
if ($res == 0) {
//Making the response error false
$response["error"] = false;
//Adding a success message
$response["message"] = "You are successfully registered";
//Displaying response
echoResponse(201, $response);
//If the result returned is 1 means failure
} else if ($res == 1) {
$response["error"] = true;
$response["message"] = "Oops! An error occurred while registereing";
echoResponse(200, $response);
//If the result returned is 2 means user already exist
}
});
現在我想知道我應該進一步做派值DB HTML表單提交時
我知道WAMP ,,但問題是我需要從PHP調用API –
糾正我,如果我錯過了一些東西,但PHP是你的API腳本。 你可以通過html表達式觸發的某些js從首頁調用它。 這樣的事情: http://stackoverflow.com/questions/31193042/how-to-execute-php-function-on-html-button-click – aslavkin