I have a form and need to submit it and save the data as a JSON file.
Is it possible?
If so how please?
Here is my simple form below.
<form action="#" method="">
<div data-role="fieldcontain">
<label for="date">Date Input:</label>
<input type="date" name="date" id="date" value="" /><br /><br />
<label for="textarea">Event:</label>
<textarea cols="40" rows="8" name="textarea" id="textarea"></textarea>
<input type="submit" value="save to json" />
</div>
</form>
<?php
$file = dirname(__FILE__).'/form-data-'.time().'-'.rand(1000,9999);
file_put_contents($file, json_encode($_REQUEST));
?>
Read this post on google http://www.ryancoughlin.com/2009/05/04/how-to-use-jquery-to-serialize-ajax-forms/
I found it by googeling "jquery serialize form"
just use json_encode function like this json_encode($_POST)
Related
Trying to get a simple isset form submit to check if the post request had been clicked from within the same page, but I can't get it working?
<?php
if(isset($_POST['btnSubmit'])){
echo "ok";
}
?>
<form id="form" name="form" action="">
<fieldset>
<legend>
<label for="fullName">Name</label>
</legend>
<input placeholder="Your name" type="text" name="fullName" id="fullName" autofocus>
</fieldset>
<fieldset>
<legend>
<label for="query">Message</label>
</legend>
<textarea placeholder="Type your Message Here...." name="query" id="query"></textarea>
</fieldset>
<input name="btnSubmit" type="submit" id="api-submit" value="Submit" />
</form>
I would expect this just to echo "ok" out onto the page? I've also cut out a lot of unessicary code from my actual work, as im just testing the principle here. But I think I musn't understand how it works, as it is sending the form contents to the URL, but just I don't know how to run seperate php code after the submit button had been clicked.
In your form you haven't specified that it is a POST request and by default, the method will be GET. You'll need to add the method="POST" to your form
<form id="form" name="form" action="" method="POST">
<fieldset>
<legend>
<label for="fullName">Name</label>
</legend>
<input placeholder="Your name" type="text" name="fullName" id="fullName" autofocus>
</fieldset>
<fieldset>
<legend>
<label for="query">Message</label>
</legend>
<textarea placeholder="Type your Message Here...." name="query" id="query">
</textarea>
</fieldset>
<input name="btnSubmit" type="submit" id="api-submit" value="Submit" />
</form>
i have created a form on my website that lets others contact me. I used a PHP script to send the email but i always get the error:
This XML file does not appear to have any style information associated with it. The document tree is shown below.
I used this code:
<form method="post" action="contact.php" enctype="text/plain">
Name*:<br>
<input type="text" name="name" placeholder='Steve'><br>
E-mail*:<br>
<input type="text" name="mail" placeholder='john#example.com'><br>
Comment*:<br>
<textarea name="comments" maxlength="400" cols="25" rows="6">
This site is awesome!
</textarea>
<br> <br>
<input type="submit" value="Send">
<input type="reset" value="Reset">
</form>
In html and this:
<?php
if($_POST["message"]) {
mail("myemail#example.com", "MCPEmaps Comment", $_POST["message"], "From: an#email.address");
}
?>
In the PHP file.
Any help?
You are POSTing comments, not message.
Change this:
<textarea name="comments" maxlength="400" cols="25" rows="6">
This site is awesome!
</textarea>
to this:
<textarea name="message" maxlength="400" cols="25" rows="6">
This site is awesome!
</textarea>
Change your html with this, As you are using $_POST["message"] but you are not passing it in your html.. So change your this line
<input type="submit" name="message" value="Send">
complete code below:
<form method="post" action="contact.php" enctype="text/plain">
Name*:<br>
<input type="text" name="name" placeholder='Steve'><br>
E-mail*:<br>
<input type="text" name="mail" placeholder='john#example.com'><br>
Comment*:<br>
<textarea name="comments" maxlength="400" cols="25" rows="6">
This site is awesome!
</textarea>
<br> <br>
<input type="submit" name="message" value="Send">
<input type="reset" value="Reset">
</form>
The PHP will not send the mail because there is no input : message
You can use :
<?php
if(isset($_POST["message"])) {
mail("myemail#example.com", "MCPEmaps Comment", $_POST["message"], "From: an#email.address");
}
?>
And
<input type="submit" name="message" />
Try this code in your contact.php file:
<?php
if($_POST["comments"]) {
mail("myemail#example.com", "MCPEmaps Comment", $_POST["message"], "From: an#email.address");
}
?>
If in case you are using in localhost you need to work lot in your php.ini file. otherwise you get result.
I would like to know how do I edit in HTML5 validation message if there is error in form.
<form method="post" id="login" action="<?php echo $_SERVER['PHP_SELF']?>">
<div class="form_settings">
<label for="textfield">Meno:</label>
<input class="zaoblene" type="text" name="meno" value="Meno....." onclick="javascript: document.forms['login'].meno.value=''" required>
<br><br>
<label for="textfield">Heslo:</label>
<input class="zaoblene" type="password" name="heslo" value="heslo....." onclick="javascript: document.forms['login'].heslo.value=''" required>
<br><br>
<input class="prihlasit" type="submit" name="prihlasit" value="prihlásit" >
</div>
</form>
You can use javascript to do it!
Here you've the jquery plugin that does this work : http://bassistance.de/jquery-plugins/jquery-plugin-validation/
I hope someone can help me. I'm a little at loss on how I can achive this: I'm trying to pass infos generated in my php to another form made in Javascript. For example, if I put my first name in the input field and click submit, then it would go to another page with the actual form and have the first name already filled there.
One thing that I did notice was that the javascript form isn't within the <form> tag, it's in a bunch of tables with some input fields. I can post what it looks like in pastebin if this can help in understanding what I mean.
Also with this script im unable to edit it, I did not make it, it is one of those script you just place on your site thats auto generated.
My form looks like this:
<form action="auto-form/index.php" method="post" name="openleads" onsubmit="return checkForm(this);">
<label for="first">First Name <span class="required">*</span></label>
<input id="first_name" type="text" name="first" applicationforms="true" /><br>
<label class="error" for="name" id="first_name_error" style="color:#F00; font-size:11px;">This field is required.</label>
<span class="fields">Zip <span class="required">*</span></span>
<input id="zip" type="text" name="zip" applicationforms="true" /><br>
<label class="error" for="name" id="zip_error" style="color:#F00; font-size:11px;">This field is required.</label>
<label for="last">Last Name <span class="required">*</span></label>
<input id="last_name" type="text" name="last" applicationforms="true" /><br>
<label class="error" for="name" id="last_name_error" style="color:#F00; font-size:11px;">This field is required.</label>
<span class="fields">Email <span class="required">*</span></span>
<input id="email" type="text" name="email" applicationforms="true" /><br>
<label class="error" for="name" id="email_error" style="color:#F00; font-size:11px;">This field is required.</label>
<input class="button" type="submit" name="send" value="Send" />
</form>
Any help is appreciated; like I said, I'm a bit at loss on what to do with this one.
php-form.php
<form action="javascript-form.php" method="post">
<input type="text" name="name" />
<input type="submit" value="Submit" />
</form>
javascript-form.php
<form action="" method="">
<input type="text" name="name" value="<?= (isset($_POST['name'])?htmlentities($_POST['name'],ENT_QUOTES):''); ?>" />
<input type="submit" value="Submit" />
</form>
Use PHP to output the POSTed values in to the value attribute of the form fields. You can also use GET variables and use javascript to parse the window.location and scrape those form values.
this seemed to get this done
<script type="text/javascript" src="http://jquery.offput.ca/js/jquery.timers.js"></script>
<script>
$(document).everyTime(1000,function(i){
if($('#ui-datepicker-div').length>0)
{
$('#first_name').val('<?php echo $_POST['first_name']; ?>');
$('#last_name').val('<?php echo $_POST['last']; ?>');
$('#zip').val('<?php echo $_POST['zip']; ?>');
$('#email').val('<?php echo $_POST['email']; ?>');
}
})
$('#first_name').val('test');
</script>
I have made following form, but it doesn't work because it doesn't send post id in post request.
<?php
require('./wp-blog-header.php');
$post = get_post($_GET['p']);
?>
<form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" >
<label>Name : </label><br/>
<input name="author" id="author" type="text"/><br/>
<label>Comment : </label><br/>
<textarea name="comment" id="comment"></textarea><br/><br/>
<input name="submit"type="submit" id="submit" value="Submit" />
<?php comment_id_fields(); ?>
<?php do_action('comment_form', $post->ID); ?>
</form>
Wordpress will drop any url parameters that it does not recognize. One way to add custom parameters in a url string is to use Add_Query_Args() function.
Take a look at Add_Query_Args Function Reference
This should solve your issue. Good luck.
With only a bit of tweaking was it possible to make it work.
<form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform">
<p><input type="text" name="author" id="author" value="" size="22" tabindex="1" />
<label for="author"><small>*</small></label></p>
<p><input type="text" name="email" id="email" value="" size="22" tabindex="2" />
<label for="email"><small>*</small></label></p>
<p><textarea name="comment" id="comment" cols="48" rows="10" tabindex="4" onFocus="clearText(this)" onBlur="clearText(this)" ></textarea></p>
<p><input name="submit" type="submit" id="submit" tabindex="5" value="Submit" />
<?php comment_id_fields(); ?>
<?php do_action('comment_form', $post->ID); ?>
</form>
Above code works (for me). Basically, you were missing an id on the form. WP apparently uses that id as part of the validation process.
So, to make it work, add id="commentform" to your form-tag, and it should work.