개발! 딱 깔끔하고 센스있게!

onClick HTML 이벤트 후 PHP 함 수 를 호출 합 니 다. 본문

개발 스크랩 메모/PHP

onClick HTML 이벤트 후 PHP 함 수 를 호출 합 니 다.

렉사이 2020. 11. 29. 22:37

목적:PHP 함수 가 파일 에서 데 이 터 를 읽 고 다시 쓰 도록 합 니 다.

제 가 PHP 를 사용 한 건 이 목적 이 었 어 요. - Fileo. - 전 PHP 에 익숙 하지 않 아 요.

솔 루 션?나 는 포럼 을 많이 해 봤 지만 정상 적 인 방식 으로 는 실 현 될 수 없다 는 것 을 알 고 있 었 다.

우 리 는 어떻게 할 까? 다른 방법 이 있 을 까? 특히 나 는 내 상황 에서?내 HTML 코드 와 PHP 코드 는 같은 페이지 에 있 습 니 다: Admin PHP.HTML 부분 입 니 다:

Add New Contact
[email protected]" required />

PHP 부분 입 니 다:

function saveContact() { $datafile = fopen ("data/data.json", "a+"); if(!$datafile){ echo ""; } else{ ... $contact_list = $contact_list . addNewContact(); ... file_put_contents("data/data.json", $contact_list); } fclose($datafile); } function addNewContact() { $new = '{'; $new = $new . '"fullname":"' . $_GET['fullname'] . '",'; $new = $new . '"email":"' . $_GET['email'] . '",'; $new = $new . '"phone":"' . $_GET['phone'] . '",'; $new = $new . '}'; return $new; } 

이 코드 들 을 보 세 요. 사람들 이 Ad Contract 단 추 를 누 르 면 saveContract 에 전 화 를 걸 고 싶 습 니 다.

필요 하 다 면 페이지 를 다시 불 러 올 수 있 습 니 다.

참고 로 저 는 페이지 에서 도 JQuery 와 HTML 5 를 사용 합 니 다.

감사합니다.

대답 하 다.

두 가지 방법 이 있 습 니 다.

첫 번 째 는 새로 고침 페이지 에 전형 적 인 양식 을 사용 하 는 것 이다.

//your_page.php  $_POST['email'], 'phone' => $_POST['phone'] ); // always return true if you save the contact data ok or false if it fails if(($saveSuccess = saveContact($data)) { $saveMessage = 'Your submission has been saved!'; } else { $saveMessage = 'There was a problem saving your submission.'; } } ?>   

Add New Contact
[email protected]" required />

두 번 째 방법 은 AJAX 를 사용 하 는 겁 니 다.

이 를 위해 서 는 폼 처 리 를 단독 파일 로 분리 해 야 합 니 다:

프로 세 스

$response = array(); if($_SERVER['REQUEST_METHOD'] == 'POST') { // if form has been posted process data // you dont need the addContact function you jsut need to put it in a new array // and it doesnt make sense in this context so jsut do it here // then used json_decode and json_decode to read/save your json in // saveContact() $data = array( 'fullname' => $_POST['fullname'], 'email' => $_POST['email'], 'phone' => $_POST['phone'] ); // always return true if you save the contact data ok or false if it fails $response['status'] = saveContact($data) ? 'success' : 'error'; $response['message'] = $response['status'] ? 'Your submission has been saved!' : 'There was a problem saving your submission.'; header('Content-type: application/json'); echo json_encode($response); exit; } ?> 

그리고 html / js 에서

Add New Contact
[email protected]" required />

Comments