submitting content to a .txt using php and staying on the same page and not go to the php page - javascript

I wanna submit/write content to a .txt file and i have to used php to do it, but i don't wanna open the php page and wanna stay on the same page.
How can I do this?
Index.html
<form action="writer.php" method="POST">
<input name="field1" type="text" />
<input name="field2" type="text" />
<input type="submit" name="submit" value="Save Data">
</form>
writer.php
<?php
if(isset($_POST['field1']) && isset($_POST['field2'])) {
$data = $_POST['field1'] . '-' . $_POST['field2'] . "|| \n";
$ret = file_put_contents('mydata.txt', $data, FILE_APPEND | LOCK_EX);
if($ret === false) {
die('There was an error writing this file');
}
else {
echo " written to file";
}
}
else {
die('no post data to process');
}
?>`

You could simply use iframes, easier alternative to AJAX.
<iframe name="panel" style="display:none;"></iframe>
<form action="writer.php" method="POST" target="panel">
<input name="field1" type="text" />
<input name="field2" type="text" />
<input type="submit" name="submit" value="Save Data">
</form>
...and as everyone here is yelling, consider learning AJAX.

create a php file with following in it.
<?php
if(isset($_POST['SubmitButton'])){ //check if form was submitted
$input = $_POST['inputText']; //get input text
file_put_contents('mydata.txt', $input, FILE_APPEND | LOCK_EX);
$message = "Success! You entered: ".$input;
}
?>
<html>
<body>
<form action="" method="post">
<?php if(isset($message)) echo $message; ?>
<input type="text" name="inputText"/>
<input type="submit" name="SubmitButton"/>
</form>
</body>
</html>

Related

Upload video Using PHP

I'm Trying this code to upload video.
but the code below is not working for me, can someone help?
Plese correct the code if required.
I dont know php well..
<?php
mysql_connect("localhost","root","123456789");
mysql_select_db("vid");
if(isset($_POST['submit']))
{
$name = $_FILES['file']['name'];
$name = $_FILES['file']['temp_name'];
move_uploaded_file($temp,"uploaded/".$name);
$url = "http://localhost/video/uploaded/$name";
mysql_query("INSERT INTO `videos` VALUE ('','$name','$url') ");
}
?>
<body>
Videos
<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file" />
<br><br>
<input type="submit" name="submit" value="Upload" />
</form>
<?php
if(isset($_POST['submit']))
{
echo "<br/>".$name." has been Uploaded";
}
?>
</body>
Thanks

submit button post not sent after using javascript

I have 3 separate forms each with their own submit buttons (removed all other lines of form code as not required):
<form action="<?php echo esc_url($_SERVER['PHP_SELF']); ?>" method="post" name="postingTopicSub" id="postingTopicSub" enctype="multipart/form-data">
<input type="button" name="submittopic" id="submittopic" value="Submit" onclick="topicPostSubmit();"/>
<form action="<?php echo esc_url($_SERVER['PHP_SELF']); ?>" method="post" name="postingTopicReply" id="postingTopicReply" enctype="multipart/form-data">
<input type="button" name="submitreply" id="submitreply" value="Submit" onclick="replyPostSubmit();"/>
<form action="<?php echo esc_url($_SERVER['PHP_SELF']); ?>" method="post" name="postingTopicEdit" id="postingTopicEdit" enctype="multipart/form-data">
<input type="button" name="submitedit" id="submitedit" value="Submit" onclick="editPostSubmit();"/>
I am using Javascript to verify the form data is filled in before submitting however because using JavaScript the POST data from the button is not being sent.
JavaScript submit for each form:
document.getElementById("postingTopicSub").submit();
document.getElementById("postingTopicReply").submit();
document.getElementById("postingTopicEdit").submit();
On my data page I use isset to make the SQL queries run are for the correct form:
// User meets all conditions and posts a topic
if (isset($_POST['topicID'], $_POST['subject'], $_POST['post_text'], $_SESSION['username'], $_POST['submittopic']) && !isset($_POST['forumID'])) {
// User meets all conditions and posts a reply
if (isset($_POST['topicID'], $_POST['post_text'], $_SESSION['username'], $_POST['submitreply'], $_POST['forumID'], $_POST['subject'])) {
// User meets all conditions and edits a post
if (isset($_POST['topicID'], $_POST['post_text'], $_SESSION['username'], $_POST['submitedit'], $_POST['forumID'], $_POST['postID'], $_POST['subject'])) {
My questions:
Is there a way to keep my method of keeping the 3 SQL queries separate
to the specific forms
Is there a way to sent the POST of the submit even though it is being
submitted through JavaScript?
Is there another way that I just haven't seen.
Thanks in advance.
EDIT:
Full form (without bbcodes)
<form action="<?php echo esc_url($_SERVER['PHP_SELF']); ?>" method="post" name="postingTopicSub" id="postingTopicSub" enctype="multipart/form-data">
<input type="hidden" name="topicID" id="topicID" value="<?php echo $topicID ?>"/>
<div class="post-subject">
Subject:
<input type="text" name="subject" id="subject" />
</div>
<div class="post-textarea">
<textarea name="post_text" id="post_text" cols="100" rows="30"></textarea>
</div>
<div class="post-button">
<input type="button" name="submittopic" id="submittopic" value="Submit" onclick="topicPostSubmit();"/>
</div>
</form>
Full JS for form validation:
function forumssubmit()
{
var subject = document.getElementById("subject").value;
var text = document.getElementById("post_text").value;
if(subject=="" || text=="")
{
alert('Please fill in the subject and text');
return false;
}
return true;
}
function topicPostSubmit()
{
if(forumssubmit())
{
document.getElementById("postingTopicSub").submit();
}
}
EDIT2::
New js:
function forumssubmit()
{
var subject = document.getElementById("subject").value;
var text = document.getElementById("post_text").value;
if(subject=="" || text=="")
{
alert('Please fill in the subject and text');
return false;
}
return true;
}
function topicPostSubmit()
{
if(forumssubmit())
{
//document.getElementById("postingTopicSub").submit();
return true;
}
}
New button:
<input type="button" name="submittopic" id="submittopic" value="Submit" onclick="return topicPostSubmit();"/>
If you're really just "using Javascript to verify the form data is filled" you can add a return to the onclick's of your submit buttons. You'll then be able to use those for submitting the form instead of Javascript.
<form action="<?php echo esc_url($_SERVER['PHP_SELF']); ?>" method="post" name="postingTopicSub" id="postingTopicSub" enctype="multipart/form-data">
<input type="submit" name="submittopic" id="submittopic" value="Submit" onclick="return topicPostSubmit()"/>
<form action="<?php echo esc_url($_SERVER['PHP_SELF']); ?>" method="post" name="postingTopicReply" id="postingTopicReply" enctype="multipart/form-data">
<input type="submit" name="submitreply" id="submitreply" value="Submit" onclick="return replyPostSubmit()"/>
<form action="<?php echo esc_url($_SERVER['PHP_SELF']); ?>" method="post" name="postingTopicEdit" id="postingTopicEdit" enctype="multipart/form-data">
<input type="submit" name="submitedit" id="submitedit" value="Submit" onclick="return editPostSubmit()"/>
Then in the javascript if you have a bad state return false.
function editPostSubmit(){
if(data == 'bad')
return false;
return true;
}
I also want to make sure you're doing data validation on the backend. We don't want to allow SQL injection on your page.
Edit:
Updated your modified code.
<form action="<?php echo esc_url($_SERVER['PHP_SELF']); ?>" method="post" name="postingTopicSub" id="postingTopicSub" enctype="multipart/form-data">
<input type="hidden" name="topicID" id="topicID" value="<?php echo $topicID ?>"/>
<div class="post-subject">
Subject:
<input type="text" name="subject" id="subject" />
</div>
<div class="post-textarea">
<textarea name="post_text" id="post_text" cols="100" rows="30"></textarea>
</div>
<div class="post-button">
<input type="submit" name="submittopic" id="submittopic" value="Submit" onclick="return forumssubmit();"/>
</div>
</form>

Double form will not submit second form

Hi I have two HTML forms when the one is submit a JavaScript function submits the second the one form works but the second doesn't I'm not sure if it is the forms or the post pages can any one help.
The one form sends an email this is sending the email correctly sending the correct data to the correct email address.
The second form is meant to upload a file it doesn't seem to be doing anything at all there are now errors displayed to the screen I have done a try catch and nothing is displayed i have also looked into the logs and nothing is displayed I'm
HTML
<div id="loginborder">
<form id ="upload" enctype="multipart/form-data" action="upload_logo.php" method="POST">
<input name="userfile" type="file" />
<input type="submit" onsubmit="alert()" value="dont press" disabled>
</form>
<div id="login">
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
<input type="hidden" name="subject" value="Can you create me a Contributors account">
<input type="text" name="first_name" id="first_name" placeholder="Name">
<input type="text" name="company" id="company" placeholder="Company Name">
<input type="checkbox" id="tc" onclick= "checkbox()">
<input type="submit" id="submit" onsubmit="alert()" name="submit" value="Register" disabled>
</form>
</div>
</div>
<?php
}
else
// the user has submitted the form
{
// Check if the "subject" input field is filled out
if (isset($_POST["subject"]))
{
sleep(5);
$subject = $_POST["subject"];
$first = $_POST["first_name"];
$company = $_POST["company"];
$therest = "First name= $first" . "\r\n" . "Company= $company" . "\r\n";
}
echo "$therest <br>";
$first = wordwrap($first, 70);
mail("careersintheclassroom01#gmail.com",$subject,$name,$therest,"subject: $subject\n");
echo "Thank you for sending us feedback";
header( "refresh:5;url=index.php" );
}
?>
</body>
</html>
javascript
<script type="text/javascript">
function alert()
{
document.getElementById("upload").submit();
}
function checkbox(){
if (document.getElementById("tc").checked == true)
document.getElementById("submit").disabled = false;
else
document.getElementById("submit").disabled = true;
}
$('input[placeholder],input[placeholder],input[placeholder],input[placeholder],input[placeholder]').placeholder();
</script>
Upload_Logo.php
<html>
<head>
</head>
</html>
<?php
$uploaddir = "./images/";
echo $uploaddir;
mkdir($uploaddir, true);
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
echo "<br />";
echo " <b>Your media has been uploaded</b><br /><br /> ";
?>
the <?php echo $_SERVER["PHP_SELF"];?> on the second form calls the php at the bottom of the page this is the one that is working it is the upload_logo.php that is not currently working any help would be much appreciated
You're trying to submit 2 forms at once. That can't work, as your browser can only be directed to 1 page at a time, so your attempt to submit the upload form with JavaScript is cancelled by the contact form being submitted. I'd suggest that you move the file input into the same form as the contact fields, and handle them both in your "the user has submitted the form" section.
Something like this should do the trick:
<?php
if (!isset($_POST["submit"]))
{
?>
<div id="loginborder">
<div id="login">
<form enctype="multipart/form-data" method="POST">
<input name="userfile" type="file" />
<input type="hidden" name="subject" value="Can you create me a Contributors account">
<input type="text" name="first_name" id="first_name" placeholder="Name">
<input type="text" name="company" id="company" placeholder="Company Name">
<input type="checkbox" id="tc" onclick="checkbox()">
<input type="submit" id="submit" name="submit" value="Register" disabled>
</form>
</div>
</div>
<?php
}
else
// the user has submitted the form
{
// Check if the "subject" input field is filled out
if (!empty($_POST["subject"]))
{
sleep(5);
$subject = $_POST["subject"];
$first = $_POST["first_name"];
$company = $_POST["company"];
$therest = "First name= $first" . "\r\n" . "Company= $company" . "\r\n";
echo "$therest <br>";
$first = wordwrap($first, 70);
mail("careersintheclassroom01#gmail.com",$subject,$name,$therest,"subject: $subject\n");
echo "Thank you for sending us feedback";
header( "refresh:5;url=index.php" );
}
if (isset($_FILES['userfile']['name'])) {
$uploaddir = "./images/";
if (!file_exists($uploaddir)) {
mkdir($uploaddir, true);
}
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile);
echo "<br />";
echo " <b>Your media has been uploaded</b><br /><br /> ";
}
}
?>
</body>
try this after $uploadfile = ...
move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile);
when you submit 2nd form e-mail would be generated becausue it triggers if(isset($_POST["subject"])) condition and the code will follow the next commands.
But when you submit 1st form, it will call onsubmit="alert(); function and that function again submits the same form because of these.
function alert()
{
document.getElementById("upload").submit();
}
so you are just triggering a never ending loop.
My solution is
<script type="text/javascript">
function alert()
{
function checkbox(){
if (document.getElementById("tc").checked == true)
document.getElementById("submit").disabled = false;
else
document.getElementById("submit").disabled = true;
}
}
$('input[placeholder],input[placeholder],input[placeholder],input[placeholder],input[placeholder]').placeholder();
</script>
I'am not 100% sure about your requirement. hope you can get the point what i'am making. gl!

Show hide part of php page

I have a php page. In the bottom there is a contact form. What I want to do is to hide the contact form when the mail is sent and instead show a thank you part. Means that when customer comes first time to the page the form show up. After submit the thank you part show up.
I have seen it done but have no clue how.
I have an idea that when the page loads it must check a variable to see if the mail was sent, but perhaps this is wrong.
When I need stuff like this, I usually name the submit button "submit" and checks if it is set...
<input type="submit" name="submit" value="Send form">
And the php (Use $_GET or $_POST accordingly)...
<?
if(isset($_POST['submit']))
{
// Show "Thank you"
}
else
{
// Show form
}
?>
When I do things like this I tend to add an button to my form and give it a name
<button type="submit" id="submit" name="submit">Submit</button>
Then in my PHP check if the form has been submitted
<?php
if(isset($_POST['submit'])) {
// Actions After Submit
}
else
{
// Load The Form
}
?>
It's rather quite simple; add exit("Message..."); after mail()
In your PHP where the mail(...) is located, you would do the following:
mail($to,$subject,$message,$headers);
exit("Thank you, your message has been sent. <a href='home.php'>Click here</a> to return to our Website.");
Here is a complete basic solution using pure PHP, since no code has been provided.
The following is meant to be run as everything inside the same file.
<?php
if(isset($_POST['send'])) {
// Prepare the email
$to = 'email#example.com';
$mail_from = $_POST['email'];
$subject = 'Message sent from website';
$message = $_POST['message'];
$name = $_POST['name'];
$header = "From: $name <$mail_from>";
// Send it
$sent = mail($to, $subject, $message, $header);
if($sent) {
exit("Thank you, your message has been sent. <a href='home.php'>Click here</a> to return to our Website.");
} else {
echo 'Sorry, your message could not be sent. The error has been logged.';
}
} // closing brace for if(isset($_POST['send']))
?>
<form method="post" action="">
<p>
<label for="name">Name</label>
<input type="text" id="name" name="name" />
</p>
<p>
<label for="email">Email</label>
<input type="text" id="email" name="email" />
</p>
<p>
<label for="message">Message</label>
<textarea id="message" name="message" rows="6" cols="30"></textarea>
</p>
<p>
<input type="submit" name="send" value="Send message" />
</p>
</form>
It's very simple, just if and else,sample form is below
<?php
if(isset($_POST['submit'])) { ?>
//write your other actions on form data
<h2>THANK YOU</h2>
<?php
} else { ?>
<form name="login" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label><h4>Username:</label>
<input type="text" name="username">
<input type="submit" name="submit" value="submit"/>
</form>
<?php }
?>

Clear input text after executing some PHP code

PHP Code :
<?php
if(isset($_POST['submit']))
{
$sent = mail($to, $subject, $email_message, $headers) ;
if($sent)
{
echo '<script> cleartext(); </script>';
}
}
?>
JavaScript :
<script type="text/javascript">
function cleartext()
{
alert(document.getElementById('name').value);
document.getElementById('name').value = '';
document.getElementById('email').value = '';
document.getElementById('phone').value = '';
document.getElementById('message').value = '';
alert(document.getElementById('name').value);
}
</script>
Source Code :
<form action="#" method="post" class="margin-bottom" target="hidden" id="contact">
<div class="row">
<div class="col-lg-6 col-sm-6">
<input type="text" placeholder="Your Name*" id="name" name="name" />
</div>
<div class="col-lg-6 col-sm-6">
<input type="text" placeholder="Your Email*" id="email" name="email" />
</div>
<div class="col-lg-6 col-sm-6">
<input type="text" placeholder="Your Phone*" id="phone" maxlength="13" name="phone" />
</div>
</div>
<textarea placeholder="Message" id="message" name="message"></textarea>
<input type="submit" id="clear" name="clear" class="button" value="Clear " onClick="cleartext();">
<input name="submit" type="submit" class="button" id="submit" onClick="MM_validateForm('name','','R','email','','RisEmail','phone','','RisNum','message','','R');return document.MM_returnValue" value="Submit ">
</form>
Problem
I want to clear all input text after sending a mail on submit.on submit I am calling cleartext() from my PHP code.but on that time I am not being able to clear my input text value.And when I call cleartext() method on clear button it's works proper.if somebody have idea about it so please help me.
You missed script type. that might be the problem.
<?php
//stuff to do
echo '<script type="text/javascript">
cleartext();
</script>';
?>
see this How to call a JavaScript function from PHP?
For button to clear fields, use type "reset" instead of "submit"
<input type="reset" value="Clear" />
And to clear the fields after submit, try redirecting
if($sent) {
header('Location: ' . $_SERVER['PHP_SELF']);
exit(0);
}
you can put a setTimeout in the onClick of the js function that clear the form
<input name="submit" type="submit" class="button" id="submit" onClick="setTimeout( MM_validateForm, 1000 )" >
Try this:
PHP:
if(isset($_POST['submit']))
{
$sent = mail($to, $subject, $email_message, $headers) ;
if($sent) $b_sent = true;
}
Javascript:
function cleartext()
{
alert(document.getElementById('name').value);
document.getElementById('name').value = '';
document.getElementById('email').value = '';
document.getElementById('phone').value = '';
document.getElementById('message').value = '';
alert(document.getElementById('name').value);
}
var bClear = <?= $b_sent? 'true':'false' ?>;
if (bClear) cleartext();
you using submit button for clear, it shouldn't work because the form will be submitting its values
and the way is you should make it return false in onclick in order to prevent submitting form.
your code should be like this:
<input type="submit" id="clear" name="clear" class="button" value="Clear " onClick="cleartext(); return false;">

Categories