Page 1 of 1

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

Posted: Thu Jul 31, 2014 4:05 am
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??

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

Posted: Thu Jul 31, 2014 4:21 am
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.

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

Posted: Thu Jul 31, 2014 7:16 am
by Celauran
Does newsletter.php have any dependencies? Check your error log; looks like the queries might be failing because $pdo isn't defined.