How to get form input value? [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm beginner for PHP. I have a form on the website and this form getting title and description values specific websites (with preg_match).
I want to add url input this form and when you add url and press submit button its auto get fields. Example
Frontend:
<input name="url">https://testdomain.com/posts/6412</input>
Backend:
$link = 'input values here';
Result:
Form fields (title and description getting) refresh or with ajax.

Create a Form and add the fields inside it..
In action, attribute add the file name where you want to post the form..
Form.php
<form method="POST" action="submit.php">
<input name="url" value="https://testdomain.com/posts/6412">
<input type="submit" name="submit" value="Submit">
</form>
submit.php
<?php
if(isset($_POST['submit'])){
$link = $_POST['url'];
}
?>
With AJAX
Form.php
<form method="POST" id="form" action="submit.php">
<div id="result"></div>
<input name="url" value="https://testdomain.com/posts/6412">
<input type="submit" name="submit" value="Submit">
</form>
<script>
$("#form").submit(function(e) {
e.preventDefault();
$.ajax({
type: "post",
url: $(this).attr("action"),
data: $(this).serialize(),
success: function(response) {
$("#result").html(response);
}
});
});
</script>
submit.php
<?php
$link = $_POST['url'];
echo $link;
?>
for ajax, you must have to add jquery library before ajax code

If you use jQuery, you can use its serialize() method to get all the form inputs.
$("#form").submit(function(e) {
e.preventDefault();
$.ajax({
type: "post",
url: $(this).attr("action"),
data: $(this).serialize(),
success: function(response) {
$("#result").html(response);
}
});
});
Note, however, that serialize() won't include the submit button itself. That's only submitted automatically in normal form submissions. If the server expects something like $_POST['submit'] to be set, you have to add it explicitly, e.g.
data: $(this).serialize() + '&submit=true',

Use this as your form, make sure to include method="post" in the form attribute section.
<form class="contact-form" method="post" action="processes/contactusform">
<div class="form-group">
<label for="name" class="sr-only">Name</label>
<input type="name" class="form-control" id="name" placeholder="Name" name="name" required />
</div>
<div class="form-group">
<label for="email" class="sr-only">Email</label>
<input type="email" class="form-control" id="email" placeholder="Email" name="email" required />
</div>
<div class="form-group">
<label for="message" class="sr-only">Message</label>
<textarea class="form-control" id="message" rows="7" placeholder="Message" name="message" required /></textarea>
</div>
<div class="form-group">
<input type="submit" id="btn-submit" class="btn btn-send-message btn-md" value="Send Message" name="submit">
</div>
</form>
In your php code, you can refer to your inputs using:
$name = $_POST['name']; //you use 'name' because the value attribute in html for that specific input is set to "name"
$email = $_POST['email'];
$message = $_POST['message'];
Your welcome

Related

Ajax 'POST' value to another page without using hidden field & form

Is that possible to pass a value without using form & hidden field?
I try the ajax but not working. Below is my code
../order/?step=0 code:
<script>
jQuery(document).ready(function($){
var step0 = [];
var item = {
pid: 123,
qty: 1
};
step0.push({0:item});
data = JSON.stringify(step0);
$("a.base-product").click(function() {
$.ajax({
type: 'POST',
url: '../order/?step=1',
data: data,
});
});
});
</script>
<a class= "base-product" href="../order/?step=1">next</a>
After I click the next link, will need to ask the data from ajax to ../order/?step=1
../order/?step=1:
print_r($_POST);
But I get Array();
Or is that able to use the $_SESSION?
<form>
<div>
<label for="title">Post title:</label>
<input type="text" id="title" name="title" value="My excellent blog post">
</div>
<div>
<label for="content">Post content:</label>
<textarea id="content" name="content" cols="60" rows="5">
This is the content of my excellent blog post. I hope you enjoy it!
</textarea>
</div>
<div>
<button type="submit">Update post</button>
</div>
<input type="hidden" id="postId" name="postId" value="34657">
</form>

Submitting a from with AJAX and mailing the submission

So I have a contact form on my website. I was able to get the submission sent with perl and it works. But I don't want the page to refresh, instead, I want it to display a success message. To make it clearer, the form would look like this after it's submitted:
What I want it to look like after the submit button is clicked
Of course, I don't want the page to refresh.
I have tried several php tutorials and I read dozens of stackoverflow questions but I couldn't get it to work, maybe that's because am completely new to php and ajax. Help is greatly appreciated
That's the html of my form:
<form id="form" name='feedback' method='POST' action='/cgi-bin/TFmail.pl' accept-charset='UTF-8'>
<input type='hidden' name='_config' value='feedback' />
<div id="texts">
<input type="text" id="name" name="name" required placeholder="Full Name"/><br>
<input type="email" id="email" name="email" placeholder="E-mail" required/><br>
<input type="tel" id="phone" name="phone" placeholder="Phone Number" required/><br>
<textarea id="message" name="comments" placeholder="Type Message Here" required></textarea><br>
</div>
<div id="buttons">
<input id="but" type="submit" value="Submit"/>
<input id="clear" type="reset" value="Clear"/>
</div>
</form>
this is a basic form submission using ajax:
$("#form").submit(function(e) {
var url = "path-to-your-script.php"; // the script where you handle the form input.
$.ajax({
type: "POST",
url: url,
data: $("#idForm").serialize(), // serializes the form's elements.
success: function(data)
{
alert(data); // show response from the php script.
}
});
e.preventDefault(); // avoid to execute the actual submit of the form.
});
taken from Submitting HTML form using Jquery AJAX

How to send two different actions in a form, with one submit button [duplicate]

This question already has answers here:
JavaScript: send multiple submitions
(4 answers)
Closed 8 years ago.
I have one actions that sends info to the journals.php and the second actions sends info to the uploads.php file. How can i do this with one submit button.
If you could show an example that would be awesome :)
<form id="login" action="journal.php?journal=journals&id=<?php echo $opened['id']; ?>" method="post" role="form">
<div class="form-group">
<label for="title">Title</label>
<input class="form-control" type="text" name="title" id="title" value="<?php echo $opened['title']; ?>" placeholder="Title">
</div>
<div class="form-group">
<label for="body">body</label>
<textarea class="form-control" name="body" id="body" rows="14" placeholder="body"><?php echo $opened['body']; ?></textarea>
</div>
<button type="submit" id="loginSubmit" class="btn btn-default">Save</button>
<input type="hidden" name="submitted" value="1">
<?php if(isset($opened['id'])) { ?>
<input type="hidden" name="id" value="<?php echo $opened['id']; ?>">
<?php } ?>
</form>
<form action="uploads.php" enctype="multipart/form-data">
<input type="file" name="file">
</form>
I have now tried to make this script.
<script type="text/javascript">
$(document).ready(function() {
$("#Login").click(function(e) {
e.preventDefault();
$.ajax({
type: "POST",
url: "uploads.php",
datatype: "html",
data: $("#loginSubmit").serialize(),
success: function(data) {
}
});
$("#loginSubmit").submit();
});
});
</script>
You can submit the text form with AJAX, wait for it to come back successfully, and then submit the file form regularly.
OR
You can make a single action file and include journal.php and uploads.php in it.

Textarea not sending on submit

Arg, this is so annoying!
I've got a form with a textarea. On submit I use a piece of php to send the data of the form to my email adress. The other data is sending fine (input) but it doesn't send the textarea along!
This is de PHP:
parse_str($_POST['stuff']);
mail("name#myemailadress.nl", "Website formulier", $name, $email, $comments);
This is the code:
<form class="form" id="form" action="" method="POST" >
<p class="name">
<input type="text" name="name" id="name" placeholder="NAAM" >
</p>
<p class="email">
<input type="text" name="email" id="email" placeholder="E-MAILADRES" >
</p>
<p class="text">
<textarea name="comments" id="bericht" placeholder="BERICHT" ></textarea>
</p>
<p class="submit">
<input type="submit" id="versturen_knop" class="submitBtn" value="VERSTUREN" >
</p>
</form>
This is the code that changes the state of the submit button for 3 seconds (message send confirmation) and triggers the PHP
$(document).ready(function(){
$('#form').submit(function(event){
$('.submitBtn').attr('value','BERICHT VERSTUURD!');
setTimeout(function(){
$('.submitBtn').attr('value','VERSTUREN');
}, 2000);
var stuff = $('#form').serialize();
jQuery.ajax({
type: 'POST',
url: 'mail.php',
data:{ 'stuff':stuff, }
});
//Prevents form submission
return false;
});
});
I hope you can help!
Try this instead
<?php
mail("name#myemailadress.nl", "Website formulier", $_POST['name'], $_POST['email'], $_POST['comments']);
?>
Try this code it works,
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<form class="form" id="form" action="" method="POST" >
<p class="name">
<input type="text" name="name" id="name" placeholder="NAAM" >
</p>
<p class="email">
<input type="text" name="email" id="email" placeholder="E-MAILADRES" >
</p>
<p class="text">
<textarea name="comments" id="comments" placeholder="BERICHT" ></textarea>
</p>
<p class="submit">
<input type="submit" id="versturen_knop" class="submitBtn" value="VERSTUREN" >
</p>
</form>
<script>
$(document).ready(function(){
$('#form').submit(function(event){
$('.submitBtn').attr('value','BERICHT VERSTUURD!');
setTimeout(function(){
$('.submitBtn').attr('value','VERSTUREN');
}, 2000);
//var stuff = $('#form').serialize();
jQuery.ajax({
type: 'POST',
url: 'mail.php',
data : $('#form').serialize(),
dataType: "json"
});
//Prevents form submission
return false;
});
});
</script>
<?php
parse_str($_POST['stuff']);
mail("aa#ss.com", "Website formulier", "$name, $email, $comments");
?>
You need to add entire contents in double quotes. I have tested it, it works fine.
Based on the circumstances, I suspect you are using a rich text editor like CKEdit or TinyMCE on your textarea.
If such is the case, you should know that these editors do not directly influence the textarea's text, and you must call a special editor-specific method to update it's contents. This method is called automatically on form submit, but for serializing and submitting forms via ajax it is not as straightforward.
If this is the case, please let me know which editor you are using and I can tell you how to correctly prepare the textarea for serialization.
Change following line
data : { 'stuff':stuff, }
to
data : stuff
or you can use
data : $('#form').serialize();
or you may try
data : {
'name' : $('#name').val(),
'email' : $('#email').val(),
'comments' : $('#bericht').val()
}
and retrieve using
$_POST['name']
$_POST['email']
$_POST['comments']

Staying on page after submitting

just a question --
How do i actually submit a form and still stay on the same page whilst the script is being executed?
I have a simple form that goes like this:
<form action="process.php" method="post" id="form">
<label for="name">Name</label>
<input type="text" name="name" id="name" required="required" />
<label for="email">Email</label>
<input type="email" name="email" id="email" required="required" />
<label for="subject">Subject</label>
<input type="text" name="subject" id="subject" required="required" />
<label for="message">Message</label>
<input type="text" name="message" id="message" required="required" />
<input type="submit" id="button" value="Submit"/>
</form>
But everytime i submit the form it just goes to process.php. I don't want that, i want it to stay on the same page, maybe even have a jquery popup saying "thank you" or something.
How do i actually stay on the same page whilst the script is being run?
Put your form processing logic on the same page as the form
Use Ajax
Redirect back to that page when process.php is done processing the form
You have to use ajax in this case.
Basically, you need to post all data using ajax. In server side, you will need to get all parameters using $_POST['name'] like you normally do in server scripting.
FORM
<form action="process.php" method="post" id="form">
<label for="name">Name</label>
<input type="text" name="name" id="name" required="required" />
<label for="email">Email</label>
<input type="email" name="email" id="email" required="required" />
<label for="subject">Subject</label>
<input type="text" name="subject" id="subject" required="required" />
<label for="message">Message</label>
<input type="text" name="message" id="message" required="required" />
<input type="button" id="button" value="Submit"/>
</form>
<div id="dialog-message" title="Thank You">
<p>Your message here</p>
</div>
​
JQUERY
$(document).ready(function(){
$('#dialog-message').dialog({
modal: true,
autoOpen: false,
buttons: {
Ok: function() {
$(this).dialog("close");
}
}
});
$('#button').on('click', function() {
var name = $('#name').val();
var email = $('#email').val();
var subject = $('#subject').val();
var message = $('#message').val();
var dataString = 'name=' + name + '&email=' + email + '&subject=' + subject + '&message=' + message;
$.ajax({
type: "POST",
url: "process.php",
data: dataString,
success: function() {
alert('');
$('#dialog-message').dialog('open');
}
});
});
});​
See the example here
Please note, dont forget to put jquery reference and also jquery ui reference
I think John Conde gave the best answer.
I would either make it so the php file that displays the form also handles the $_POST or if you can't do it that way then do it in Ajax with a code similar to this:
$("#form").bind("submit", function() {
var nameTxt = $("#name").val();
var emailTxt = $("#email").val();
$.post('process.php',
{
name: nameTxt,
email: emailTxt
},
function(data) {
if (data == 'ko')
alert('Could not submit...');
else {
alert('Thank you for your message.');
}
});
return false;
}
What this does is that it "blocks" the regular submit of the form when you click the submit button, retrieve input values and sends them to the process.php script.
It assumes that your "process.php" does an
echo "ko";
if there is an error. You can do some form cleanup after the form has been successfully sent by reseting the inputs:
$("#name").val('');
$("#email").val('');
Assuming you're using jquery:
$(document).ready(function(){
$('#form').submit(function(e){
// do some ajax request to process.php here...
// stop the form doing its default action of submitting directly to process.php
e.preventDefault();
});
});
try something like this
action="<?php print $_SERVER["PHP_SELF"]; ?>" (or) action=""
EDIT:
<?php
if(isset($_POST['Submit'])){
//do your validation or something here
header("location:Process.php");
}
I targeted an iframe to solve the problem, and it worked perfectly.
<form name="contact" role="form" method="post" action="contact.php" target="my_iframe">
Feild 1: <input name="feild1" id="feild1" type="text" placeholder="Lorem ipsum dolor"><br/><br/>
Feild 2: <input name="feild2" id="feild2" type="text" placeholder="Lorem ipsum dolor"><br/><br/>
Feild 3: <textarea name="feild3" id="feild3" rows="5" type="text" placeholder="Lorem ipsum dolor, sit amet."></textarea><br/><br/>
<input type="submit">
</form>
<!-- target iframe to prevent redirection to 'contact.php' -->
<iframe name="my_iframe" width="1" height="1" style="border:none"></iframe>
Literally:
<form action="process.php" method="post" id="form" target="_blank">
^^^^^^^^^^^^^^^^
This will make the browser stay on the same page, even if the form is submitted.
But you will be more likely looking for AJAX instead, some Introduction.
To do that you'll need jquery ajax, see here $.post
You can do something like this (with jQuery, untested)
$(function() {
$("#form").submit(e) {
// Prevent regular form submission
e.preventDefault();
// Submit the form via AJAX
var $form = $("#form");
$.post(
$form.attr("action"),
$form.serialize(),
function() { alert("Done!"); }
);
}
}

Categories