Hello, I'm new learner of PHP language. I create register form, I can insert the data from data in DB. But I want to display message when I click on "Submit" button "Recors insert successfully!" on same form. Please give any solution for this as early as possible.
when you want it to be on the same form/page the only way i can think of is using AJAX. Tutorials for ajax can be found at fi http://www.ajaxlessons.com/
You can do it this way: create an iframe make the submit button submit the form data to your PHP script via the iframe and have the PHP which you submit to say it's inserted in the iframe
after inserting the data in your database, redirect back to the same form using the header function.. now to print your message, you could use iether a get variable (url query string) or a session variable.. to use a get variable, just add something like ?success=true on the URL when you redirect back to the form, then on the form, you will have something to check if the get variable success is set and is equal to true, like this... if (isset($_GET['success']) && $_GET['success'] == 'true') { echo "Record inserted successfully"; } using session variables is just the same, create a session variable before you redirect, then on the form you check if the session variable exist and then print your message, after printing your message delete the session variable for a better effect, use the unset function to delete the variable.. did you get it?
by the way, my solution needs to redirect back and forth to the form, if you want it not to redirect and save the data asynchronously, use AJAX...
Other way is doing it with javascript, try it: <script type="text/javascript"> function Confirm(form){ alert("Record insert successfully!"); form.submit(); } </script> <form name="form" method="post" action=""> Your Email:<br> <input name="email" type="text" /> <input type="button" name="Submit" value="Submit" onClick="Confirm(this.form)"> </form> PHP: It will display an alert with the text "Record insert successfully!" when the user press the submit button. Other way is: <? if (!$HTTP_POST_VARS){ ?> <form name="form" method="post" action=""> Your Email:<br> <input name="email" type="text" /> <input type="button" name="Submit" value="Submit"> </form> <? } else { echo "Record insert successfully!"; } ?> PHP: Good luck
u can use this source code : <?php $connection = mysql_connect("localhost", "root", ""); // Establishing Connection with Server $db = mysql_select_db("colleges", $connection); // Selecting Database from Server if(isset($_POST['submit'])){ // Fetching variables of the form which travels in URL $name = $_POST['name']; $email = $_POST['email']; $contact = $_POST['contact']; $address = $_POST['address']; if($name !=''||$email !=''){ //Insert Query of SQL $query = mysql_query("insert into students(student_name, student_email, student_contact, student_address) values ('$name', '$email', '$contact', '$address')"); echo "<br/><br/><span>Data Inserted successfully...!!</span>"; } else{ echo "<p>Insertion Failed <br/> Some Fields are Blank....!!</p>"; } } mysql_close($connection); // Closing Connection with Server ?> Code (markup):
I wouldn't recommend anyone uses that code if they care about the integrity of their site. EDIT, Why the hell did you drag up an 8 year old thread to post that rubbish?
Well... The code he posted seems like it was written in 2008,so he's keeping with the rest of the thread...?
I'm learning PHP and MYSQL at the moment, so I'm curious as to why the code from Faris Basalamah is bad. Is it because everything is in one file rather than being split into various files? Or is it something else? And could you point me to an up to date website covering this area as I don't want to get into any bad programming habits before I even begin! TIA
It's bad because he uses mysql_ to connect to the database. That function-call has been outdated for 10+ years, and should NEVER be used on a live production site. We have two other options, mysqli_ and PDO - personally, I prefer the latter, as it more or less demands that you create proper, parameterized queries, and allows for easy bundling, repeats and modifications to queries, making it easier to change the variables and update multiple records at the same time. That was the main problem with the code - also, while he used mysql_ he did NOTHING to prevent code-injections from user-input. You NEVER, EVER trust anything a user inputs into a form, adds to an URL, or any other means - user input is ALWAYS bad, although you usually have to deal with it frequently. PDO (and mysqli_) has functionality to prevent anything malicious reaching the database.
Ah, I didn't notice the mysql bit at all. Checking what I've been working with, all my code has mysqli. And I've been learning (and using) escape strings. There's a lot of security checks to do! Always worried I might miss something. But thanks for pointing out what is wrong with that code
Yes - the benefit of using PDO is that you don't have to dick around with escaping strings and such - it's handled by the PDO-class, which allows you to do something simple like this: $stmt = $dbh->prepare("INSERT INTO contacts (`name`,`number`) VALUES (:name,:number)"); $stmt->execute([':name'=>$_POST['name'],':number'=>$_POST['number']]); PHP:
Hmm. I'll have another look into PDO. I didn't fully understand it which is why I stuck with mysqli. But if it's going to make things simpler in the long run then I'll give it another shot.