wp_enqueue_script( 'bundle', plugins_url( '/bundle.js', __FILE__ ), array('jquery'), '1.11', true );
wp_localize_script( 'bundle', 'MyAjax', array(
'ajaxurl' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('myajax-nonce')
) );
add_action( 'wp_ajax_cr_new_patient', 'cr_new_patient' );
function cr_new_patient() {
$nonce = $_POST['nonce'];
if(wp_verify_nonce( $nonce, 'myajax-nonce' )){
$id = sanitize_text_field($current_user->user_login);
$ur = sanitize_text_field($_POST["patient_ur"]);
$dob = sanitize_text_field($_POST["patient_dob"]);
$gender = sanitize_text_field($_POST["patient_gender"]);
$ident = sanitize_text_field($_POST["patient_ident"]);
$other = sanitize_text_field($_POST["patient_other"]);
$loc = sanitize_text_field($_POST["patient_loc"]);
$query="INSERT INTO patient (
'ur', 'dob', 'gender', 'ident', 'other', 'loc'
) VALUES (
'$ur', '$dob', '$gender', '$ident', '$other', '$loc',
)";
$mysqli = new mysqli("xxx","xxx","xxx","xxx");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
handleSubmit(data) {
jQuery.post(
MyAjax.ajaxurl,
{
'action': 'cr_new_patient',
'patient_ur': data.patient_ur,
'patient_dob': data.patient_dob,
'patient_gender': data.patient_gender,
'patient_ident': data.patient_ident,
'patient_other': data.patient_other,
'patient_loc': data.patient_loc,
'nonce' : MyAjax.nonce
},
function(response){
alert('The server responded: ' + response);
}
);
}
constructor(...args) {
super(...args);
this.state = {
form: <First/>
};
}
{this.state.form}
<Button onClick={() => this.setState({ form: <Second/> })} >Create a new Patient</Button>
getFormData() {
var data = {
name: this.refs.name.getValue(),
mail: this.refs.mail.getValue(),
text: this.refs.text.getValue()
};
this.handleSubmit(data);
},
handleSubmit(data) {
console.log("Hi from handleSubmit");
console.log(data.name);
console.log(data.mail);
console.log(data.text);
jQuery.post(
MyAjax.ajaxurl,
{
'action': 'send_message',
'name': data.name,
'email': data.mail,
'message': data.text
},
function(response){
alert('The server responded: ' + response);
}
);
},
wp_enqueue_script( 'bundle', plugins_url( '/bundle.js', __FILE__ ), array('jquery'), '1.0', true );
wp_localize_script( 'bundle', 'MyAjax', array(
'ajaxurl' => admin_url('admin-ajax.php')
) );
add_action( 'wp_ajax_send_message', 'do_send_message' );
add_action( 'wp_ajax_nopriv_send_message', 'do_send_message' );
function do_send_message() {
if ( isset($_POST['name']) && isset($_POST['email']) &&isset($_POST['message']) ) {
$name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message'];
$send_to = "antibioticvz@gmail.com";
$subject = "Question from " . $name;
$success = wp_mail($send_to,$subject,$message);
if ($succsess) return true;
if (!$succsess) return false;
}
}