How do I put something in URL on homepage /u=yes ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

How do I put something in URL on homepage /u=yes ?

Post by simonmlewis »

I am adding a form for users to submit to a newsletter.
This sounds like the dumbness "maverick" task, but I need the page to turn over, the script to run the INSERT, and the URL to show something so I can query a variable and add "thanks for submitting...".

I would love to do it in AJAX. So they click "Send", and that little box just changes.

How could this be done in Ajax??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I put something in URL on homepage /u=yes ?

Post by simonmlewis »

I'm trying this, assuming comment.php is the external file to which you are submitting data:
http://malsup.com/jquery/form/

This is my main page:

Code: Select all

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> 
    <script src="http://malsup.github.com/jquery.form.js"></script> 
 
    <script> 
        // wait for the DOM to be loaded 
        $(document).ready(function() { 
            // bind 'myForm' and provide a simple callback function 
            $('#myForm').ajaxForm(function() { 
                alert("Thank you for your comment!"); 
            }); 
        }); 
    </script> 
    
 <form id='myForm' action='/newsletter.php' method='POST'> 
  <input type='text' name='email' class='newsletteremail'>
  <input type='submit' value='Send'>
  </form>

This is my version, but as newsletter.php:

Code: Select all

<?php
$email = $_POST['email'];
$query = ("SELECT * FROM newsletter WHERE email =:email");
$result = $pdo->prepare($query);
$result->execute(array(':email' => $email));
$num_rows = $result->rowCount();
if ($num_rows == 0) 
  { 
  $query = "INSERT INTO newsletter =:email";
  $result = $pdo->prepare($query);
  $result->execute(array(':email' => $email));
  }
  
  ?>
But it's not submitting anything.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I put something in URL on homepage /u=yes ?

Post by Celauran »

Does newsletter.php have any dependencies? Check your error log; looks like the queries might be failing because $pdo isn't defined.
Post Reply