1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Message display after click on submit button on same page.

Discussion in 'PHP' started by sweetu, Jun 26, 2008.

  1. #1
    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.
     
    sweetu, Jun 26, 2008 IP
  2. Edynas

    Edynas Peon

    Messages:
    796
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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/
     
    Edynas, Jun 26, 2008 IP
  3. knukk

    knukk Peon

    Messages:
    43
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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
     
    knukk, Jun 26, 2008 IP
  4. bokiatenxi

    bokiatenxi Peon

    Messages:
    27
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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?
     
    bokiatenxi, Jun 26, 2008 IP
  5. bokiatenxi

    bokiatenxi Peon

    Messages:
    27
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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...
     
    bokiatenxi, Jun 26, 2008 IP
  6. dannet

    dannet Well-Known Member

    Messages:
    863
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    153
    #6
    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
     
    dannet, Jun 26, 2008 IP
  7. Faris Basalamah

    Faris Basalamah Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #7
    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):
     
    Faris Basalamah, Apr 24, 2016 IP
  8. malky66

    malky66 Acclaimed Member

    Messages:
    3,996
    Likes Received:
    2,248
    Best Answers:
    88
    Trophy Points:
    515
    #8
    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?
     
    malky66, Apr 25, 2016 IP
  9. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #9
    Well... The code he posted seems like it was written in 2008,so he's keeping with the rest of the thread...?
     
    PoPSiCLe, Apr 25, 2016 IP
    qwikad.com likes this.
  10. FoxIX

    FoxIX Well-Known Member

    Messages:
    211
    Likes Received:
    10
    Best Answers:
    1
    Trophy Points:
    140
    #10
    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
     
    FoxIX, May 12, 2016 IP
  11. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #11
    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.
     
    PoPSiCLe, May 12, 2016 IP
  12. FoxIX

    FoxIX Well-Known Member

    Messages:
    211
    Likes Received:
    10
    Best Answers:
    1
    Trophy Points:
    140
    #12
    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 :)
     
    FoxIX, May 12, 2016 IP
  13. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #13
    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:
     
    PoPSiCLe, May 12, 2016 IP
  14. FoxIX

    FoxIX Well-Known Member

    Messages:
    211
    Likes Received:
    10
    Best Answers:
    1
    Trophy Points:
    140
    #14
    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.
     
    FoxIX, May 12, 2016 IP