I designed plain html site,with one enquiry form.It has some fields like name ,address etc.
Onclick of submit button,i wanted this data to go into the database.
I created database and table using phpmyadmin on XAMPP server.
My code is as follows(hello.php):
<script>
function insert_db()
{
<?php
$username = $_POST['name'];
print ($username);
mysql_connect("localhost","root","");
mysql_select_db("addressbook");
mysql_query("insert into colleague values ('$username')");
?>
}
</script>
<form method="post">
<input type="name" name="name" /><br />
<input type="submit" name="mysubmit" value="Submit" onclick="insert_db()"/>
On click of submit button ,two entries are made into the database.One is blank and other is entered value.
how to call the function only once??Is there any other way out..
You cannot, I repeat CANNOT, combine JS and PHP in this way.
Right-click the page, select View Source, and you will clearly see why.
PHP is run BEFORE the browser gets the page.
Therefore, you get an empty record when you load the form, then a filled one when you submit it because $username is defined that time.
You can't mix php and javascript like this. You see, the PHP will always be executed server-side, regardless where it is in the file (PHP does not know JavaScript). Therefore, the first time you load the page, it will insert an empty row (since $_POST['name'] is empty). Then, if you click submit, it will do nothing, since the JavaScript code ist empty, submit the form to the server and evaluate the PHP code again, this time with the desired effect.
A proper way to do this would be:
<?php
if (!empty($_POST['name'])){ // check if form values are there
$username = $_POST['name'];
print ($username);
mysql_connect("localhost","root","");
mysql_select_db("addressbook");
mysql_query("insert into colleague values ('$username')");
}
?>
<form method="post">
<input type="name" name="name" /><br />
<input type="submit" name="mysubmit" value="Submit" />
</form>
You might also want to work your way through a PHP Tutorial first
Related
I have a html page where the user can input some text, it is then posted to a php file and then stored in a database.
<form action="postphp.php" method="post" enctype="multipart/form-data">
<center><input id="postTitleStyle" type="text" name="title" size="100" maxlength = "180" ></center>
<center><textarea id="postTextStyle" name="Text" rows="10"cols="96" maxlength = "40000"><?php echo $text;?></textarea></center>
<center><input id="postTagStyle" type="text" name="tags" size="100" maxlength = "900" ></center>
<center><input type="submit" class = "Post2" value="post"></center>
</form>
Above is my current code for posting the data in the text field to a php file. I want to be able to click a button that when clicked will not go to the php file it will be stored and then when the user clicks the submit button it is posted. For example the user clicks it, a one is stored and then sent later when the user clicks the submit button after they are finished filling in other details. Is this possible?
P.S. I want to avoid Javascipt as much as possible for the moment, so if there is a non-java way of doing it then it would be much appreciated.
Many thanks, Jack.
There are two easy solutions to this problem without using Javascript. I'm assuming by your wording that you can currently post a form, but you don't know how to do so without leaving the current page. That's what I'll be answering below, but please note that there is no way to post a form without reloading at all without Javascript.
Solution 1: Put the PHP code into the same page the form is on and change the form tag to: <form action="" method="post" enctype="multipart/form-data">
A blank action field will cause it to run the PHP on the current page. You will likely need to look into using isset($_POST['submit']) in PHP, which will check whether the submit button has been clicked on before running that particular PHP code. You can read more about that HERE.
Solution 2:
In the postphp.php file that's currently linked to in your action field of your form, you could use a PHP header that will redirect the user after the PHP code is ran.
E.g.
<?php
{All your form processing code goes here}
header('location: my_form_page.php');
?>
In this example, my_form_page.php is the page on which your form is on. You can read more about headers in the answer of THIS question.
Hopefully this helps a bit.
$title = $_POST['title'];
$text= $_POST['text'];
$tags = $_POST['tags'];
mysql_query("INSERT INTO `table_name` (`colname1`,`colname2`,`colname3`) VALUES ('$title,'$text','$tags')");
$id = mysql_insert_id();
if($id){
echo "inserted";
}else{
echo "Not inserted";
}
For this you need to use Ajax (JavaScript will be used) because you need a button which send data to server without form submission and page reload it can be easily achieved using Ajax.
<html>
<script>
function changeText()
{
document.getElementById("input1").value = <?php echo '"'.$_POST['input'].'"'; ?>;
return true;
}
</script>
<form name="mainform" action="" method="post">
<input type="text" name="input" id="input1" />
<input type="submit" onclick = "changeText()" name="Submit" value="Submit!" />
</form>
<html>
i have this code here. can you make it work as intended ?
everytime i click Submit! i want to change the value of the textarea to the last input the user inserted.
PHP code is parsed by a PHP interpreter before any HTML output is sent to the browser.
If your form action is the same page and the same form will be shown before and after submission, then you can let PHP print the value of the input field directly into it.
<input type="text" name="input" id="input1" value="<?php echo htmlspecialchars($_POST['input']);" />
If you're trying to revert the value of this input field whenever a user clicks the submit button, then your code (even if it's prone to code injection) should work but this is useless since the page will be requested again when submit is clicked.
I assume you need to fill in
action=""
By the name of your file, like
action="myFile.php"
Few tips :
NEVER trust the user. The user can manually change the value of the input and send some dangerous values in your $_POST variable. You need to check it using filter_input() by example.
Like #Charles said this is pretty simple problem, use google next time.Here for example
I have this form in index.php
<form action="result.php" method="post">
<input type="text" name="word" class="tbox">
<input type="submit" name="submit" value="Generate Now">
</form>
In result.php i am checking whether the input matches all validation condition and generating the output. Now, if it doesnt match, i ve to throw an error near my text box. Is that possible through php?
Using PHP, you may try like below
<input type="text" name="word" class="tbox"> <?php if(isset($_POST['word']) && $_POST['word'] == ''){ echo 'YOUR ERROR MESSAGE'; } ?>
a few things, one you can just post against the page itself not great to have DB functions and the like in user reachable areas so check out includes and use them to reference the single page.
Then make a simple variable and slide it into your form as such
<? echo $error ?>
and set error to something via the php script imported from result.php, or you may want to learn about sessions and get variables if combining the files via include isn't on the table.
Once its working then say make a div box as such
<? if (isset($error)){?><div class="errorbox"><? echo $error?></div><? }?>
The form below can contains different elements of text fields, drop down and selection boxes, which allows the user to update his profile. The process of updating MySQL fields is being done after form submits.
<form method="post">
My Name: <input name="myname" type="text" value="<?php echo $_SESSION['SESS_MY_NAME']; ?>" /><br />
My Email: <input name="email" type="text" value="<?php echo $_SESSION['SESS_EMAIL']; ?>" /><br />
<input type="submit" name="Submit" value="Update" />
<?php
if (isset($_POST['Submit'])) {
// MySQL update
;
;
;
$result=mysql_query($sql);
}
// if successfully updated, make form refresh.
if($result){
;
;
;
}
?>
</form>
I want to refresh only the form, so that the user will stay in the same page and will see the changes that he made (i.e. What do I need to put under the comment in the code “if successfully updated, make form refresh”?).
I cannot use header("location: samepage.php"); because I have too many HTML codes involved in between and before.
Appreciate any help,
Move the isset($_POST['submit']) check to the top.
Your current (intended) process is as follows:
Retrieve form data from DB/Session/etc.
Display as HTML
User submits form
Repeat steps 1/2
Write data to DB
Force refresh
Repeat 1/2 again
If you move this check to the top, the process is changed to:
Retrieve form data from DB
Display as HTML
User submits form
Write data to DB
Repeat 1/2 (it will now retrieve the updated information and display correctly).
This is the absolute simplest way you can do it. It doesn't take into account Post/Redirect/Get or updating the session so multiple DB reads are unncessary, etc.
Why don't you use jQuery AJAX to submit and verify the form then output the result in chosen div element.
More on that available at nettuts+
Such helpful. Much wow.
Thankfully I was able to solve this on my own after some intense googling since no one else seemed to have an answer. For anyone looking to accomplish the same thing, all you have to do is add the following code to the "Additional Settings" section of your CF7 form configuration:
on_sent_ok: "location = 'http://domain.com/contact';"
Just replace the URL with the URL of your form page and presto, it'll automatically refresh back to that page when the form is submitted!
I'm working on a site that gives the users of the site pre-written letters to send to places. All the user has to do is fill out a form and hit continue, then whatever information the user put into the form (like name for example) gets plugged into a pre-written letter on a printable page. A basic example would be if the form asks for Name then you hit continue, on the printable page, it would say:
Hi, my name is Zach.
I'm using a php based content management system so it should be in php. I know this is a very simple thing to do for those who know how to do it, I unfortunately don't. Thank you in advance for your help!
Suppose you have this form:
<form action="preview.php" method="POST" >
<input type="text" name="name" />
<input type="submit" value"Print" />
</form>
When you hit submit, the values of all the fields (input in this case, but also textarea, selects, etc) are save to the POST array (or GET if you set method="GET").
You access the POST and GET arrays from the preview.php page (where you want to print the name in this example) with code like this:
<?php
$name = $_POST['name'];
?>
<p>Hi, my name is <strong><?=$name?></strong>.</p>
in your first page:
<form action="letter.php" method="get">
<input type="text" name="personsName"></input>
<input type="submit" value="submit">
</form>
Then in letter.php do this:
<?php
$firstname = $_GET['personsName'];
echo "My Name is" .$firstname;
?>
That ok? :)