Here is my form
HTML
<div id="contactus">
<form action="carrer_.php" method="POST" ENCTYPE="multipart/form-data" class="contactUs" id="send_cv" target="carrer_iframe">
<label> Name
<input type="text" name="name" />
</label>
<label> Email
<input type="text" name="email" />
</label>
<label> Mobile
<input type="text" class="mobile" name="mobile" />
</label>
<label> Cv
<input type="file" id="cv" name="file" />
</label>
<label> Other Info
<textarea name="message" cols="1" rows="1"></textarea>
</label>
<input type="submit" value="send" />
</form>
<div id="iframe_result"></div>
<iframe name="carrer_iframe" id="carrer_iframe" src="carrer_.php" style="width;0;height:0;display:none;" ></iframe>
</div>
Js
$('#send_cv').submit( function(){
$("#carrer_iframe").contents().html("") ;
$("#iframe_result").load(function(){
alert( 1 ) ;
response = $("#carrer_iframe").contents().find("body:last").text();
alert( response ) ;
$("#iframe_result").html(response).show();
});
} ) ;
After submit the form ( Nothing happened??? )
How can i get the result from the hidden iframe and alert it && Or what is my error
Best regards
There seems to be nothing preventing the form from submitting normally. Try passing the event from the submit function and calling .preventDefault.
$('#send_cv').submit( function(event){
// Preventing the default form submission action
event.preventDefault();
$("#carrer_iframe").contents().html("") ;
$("#iframe_result").load(function(){
alert( 1 ) ;
response = $("#carrer_iframe").contents().find("body:last").text();
alert( response ) ;
$("#iframe_result").html(response).show();
});
} ) ;
Edit:
As far as I know .show() only effects the display attribute. Therefore setting the wdith/height to 0 will mean the iframe will never show.
Related
I have a custom WordPress page template where I wrote a basic form structure like this:
<div class="my-form">
<h2>WILL YOU ATTEND?</h2>
<form action="#" autocomplete="off" method="POST">
<input class="input" type="text" spellcheck="false" name="name" placeholder="Name" required>
<input class="input" type="email" spellcheck="false" name="email" placeholder="E-mail" required>
<label class="ans">Yes
<input type="radio" name="radio">
<span class="checkmark"></span>
</label>
<label class="ans">No
<input type="radio" name="radio">
<span class="checkmark"></span>
</label>
<button> SUBMIT </button>
</form>
</div>
Now once the submit button is clicked, I'm trying to figure out how to create a table in WordPress database, and insert the form data into it using $wpdb.
PS: I left the action attribute empty because I don't know how to go about this exactly.
There are several ways to do that. Below I just showed you one way:
I Assume your table name is wp_customform which has id, name, email, and radio columns.
Now you've to modify your HTML code like the below. I added some JS too.
<div class="my-form">
<h2>WILL YOU ATTEND?</h2>
<form class="custom-form-class" action="#" autocomplete="off" method="POST">
<!-- Action is here but hidden. This will be use in php side -->
<input type="hidden" name="action" value="sample_custom_form_action">
<input class="input" type="text" spellcheck="false" name="name" placeholder="Name" required>
<input class="input" type="email" spellcheck="false" name="email" placeholder="E-mail" required>
<label class="ans">Yes
<input type="radio" name="radio">
<span class="checkmark"></span>
</label>
<label class="ans">No
<input type="radio" name="radio">
<span class="checkmark"></span>
</label>
<button> SUBMIT </button>
</form>
</div>
<script>
// Javascript to send ajax request
jQuery(document).on('submit', '.custom-form-class', function(e){
let formData = jQuery(this).serialize();
// Change ajax url value to your domain
let ajaxurl = 'http://YOURSITE.COM/wp-admin/admin-ajax.php';
// Send ajax
jQuery.post(ajaxurl, formData, function(response) {
alert('Got this from the server: ' + response);
});
});
</script>
That pieces of code will send your form data to the PHP side. You have to use the below code to process the form data in your function.php file.
add_action( 'wp_ajax_sample_custom_form_action', 'prefix_save_custom_form_data' );
add_action( 'wp_ajax_nopriv_sample_custom_form_action', 'prefix_save_custom_form_data' );
function prefix_save_custom_form_data(){
global $wpdb;
// Get the values
$name = isset( $_POST['name'] ) ? sanitize_text_field( $_POST['name'] ) : ''; // Empty value if data not set
$email = isset( $_POST['email'] ) ? sanitize_email( $_POST['email'] ) : ''; // Empty value if data not set
$radio = isset( $_POST['radio'] ) ? sanitize_text_field( $_POST['radio'] ) : ''; // Empty value if data not set
// Assume your table name is wp_customform
$your_table_name = $wpdb->prefix . 'customform';
// Insert into database
$wpdb->insert(
$your_table_name,
array(
'name' => $name,
'email' => $email,
'radio' => $radio,
)
);
// Send insert id as ajax response
echo $wpdb->insert_id;
// Use die to stop the ajax action
wp_die();
}
The codes are not tested but they should work. I am attaching some reference links below:
Ajax overview: https://codex.wordpress.org/AJAX_in_Plugins
For Insert: https://developer.wordpress.org/reference/classes/wpdb/insert/
For create table: https://developer.wordpress.org/reference/functions/dbdelta/
i've got a problem with the load Methode from jQuery.
Here my code:
$('#form').submit(function( event ) {
$('.response').load('responseData.php?'+
$('#form').serialize());
event.preventDefault();
});
<form id="form">
<input type="text" name="title" id="title" />
<input type="text" name="text" id="text" /><br>
<input type="submit" value="Speichern" class="submit"/>
</form>
<div class="response"></div>
The file responseData.php exists. But when I click submit, i get an '500 internal error'.
The path is correct it is:
http://domain.de/data/responseData.php?title=test&text=test
Do you know what's going on here? I'm really desperated.
Thank you!
I would like to make the PHP echo I have appear in the div "preview". I want this because I don't want the page to refresh but to make the div "formbox" disappear and to show the echo in the "preview" div.
Does someone know what I am doing wrong?
HTML
<div id="preview"> </div>
<div id="formbox">
<form id="form" method="post" action="contactengine.php">
<label for="Name">Name:</label>
<input type="text" name="Name" id="Name" />
<label for="City">City:</label>
<input type="text" name="City" id="City" />
<label for="Email">Email:</label>
<input type="text" name="Email" id="Email" />
<label for="Message">Message:</label><br />
<textarea name="Message" rows="20" cols="20" id="Message"></textarea>
<input type="submit" name="submit" value="Submit" class="submit-button" />
</form>
</div>
jQuery
$('document').ready(function(){
$('#form').ajaxForm( {
target: '#preview',
success: function() {
$('#formbox').slideUp('fast');
}
});
});
PHP
if ($success){
echo "<h1>Thank You!";
}
Just use the result.
$('document').ready(function(){
$('#form').ajaxForm( {
target: '#preview',
success: function( result ) {
$('#formbox').slideUp('fast');
$('#preview').html(result);
}
});
});
When I submit a form using the attached code, instead of the message appearing in the div, the screen is refreshing itself and it is holding the completed form in the browser cache. I have obviously got it wrong somewhere and would be grateful if someone could point out my error. I have posted the relevant code, but if there is something I have missed, then please let me know.
In firebug, I can see the correct data that is being returned in the post tab.
I wasn't sure of the best place to post, so if this is incorrect, admin, please amend as you see fit. many thanks.
jquery code
//Begin function to submit report form
$(function(){
$(".frmreport").submit(function(){
var formdata = $(this).serialize();
$ajax({
type: "POST",
url: "../frm10010.php",
data: formdata,
dataType: "json",
success: function(msg){
$("#report_result").html("You have succesfully submitted your report. Thank you.");
}
});
return false;
});
});
// End function to submit report form
frm10010.php
<?php
$dept = mysql_real_escape_string($_POST['dept']);
$name = mysql_real_escape_string($_POST['name']);
$email = mysql_real_escape_string($_POST['email']);
$position = mysql_real_escape_string($_POST['position']);
$feedback = mysql_real_escape_string($_POST['feedback']);
$form = array('dept'=>$dept, 'name'=>$name, 'email'=>$email, 'position'=>$position, 'feedback'=>$feedback);
$result = json_encode($form);
echo $result;
?>
html
<div id="report_result"></div>
<div id="formShow">
<form class=frmreport" method="post" class="webform">
<fieldset>
<legend><span class="subtitle">Submit Technical Report</span></legend>
<label for="dept">Department</label>
<input id="dept" name="dept" class="text" type="text" />
<label for="name">Full Name:</label>
<input id="name" name="name" class="text" type="text" />
<label for="email">Email address:</label>
<input id="email" name="email" class="text" type="text" />
<label for="position">Position:</label>
<input id="position" name="Position" class="text" type="text" />
<label for="feedback">Problem:</label>
<textarea name="feedback" cols="22" rows="5"></textarea>
</fieldset>
<input class="submit" type="submit" name="submit" value="Submit Report" />
<input class="cancel" type="reset" name="cancel" value="Clear Report" />
</form>
</div>
You have couple of errors here
$ajax({ should be $.ajax({
Second you have an error in the form class
<form class=frmreport" method="post" class="webform">
should be
<form class="frmreport" method="post" class="webform">
Don't use
$(".frmreport").submit(function(){
Instead use
$("#sub_btn").click(function(){
And in html
<input class="submit" type="submit" name="submit" value="Submit Report" />
Change it to
<input class="submit" id="sub_btn" type="button" name="submit" value="Submit Report" />
Another way to do it making submit handler false. But above solution will work here. There is also . is missing in ajax call.
HTML code :
I echo this content in php file, In result i get only the 'Contact Us' Remaining part does not display the html form
<div > Contact Us
<form name="profile" action="" method="post">
Name : <input type="text" name="fullname" id="fullname" />
Email : <input type="text" name="email" id="email" />
Mobile : <input type="text" name="mobile" id="mobile" />
</form>
</div>
Jquery code response :
$.post("contact_us.php",{ user_id:$('#user_id').val(),typ:'contact_us' },
function(data) {
$('#contact_us').html(data);
});
In contact_us id does not write the contact us form, Please anyone help me
Have you tried using Firebug?
http://getfirebug.com/
That will let you see what the script is returning.
UPDATE:
You are referencing a container with ID = "contact_us"
$('#contact_us').html(data);
You need to set the ID of the div you want to replace the content in.
<div id="contact_us"></div>
Your HTML isn't valid. You need to close the div tag
<div >
Contact Us
<form name="profile" action="" method="post">
Name : <input type="text" name="fullname" id="fullname" />
</form>
</div>