14121

Is there a way I can find out if my data string is being passed on to my php function

Question:

I am calling a ajax form submit script as given below :

$(function() { $('.simple-success').hide(); $("#submit_button").click(function() { var firstname=$("input#Firstname").val(); var lastname=$("input#Lastname").val(); var email = $("input#email").val(); var title = $("input#title").val(); var organization = $("input#organization").val(); var dataString = 'firstname='+ firstname + '&lastname=' + lastname + '&email=' + email + '&title=' + title + '&organization=' + organization; $.ajax({ type: "POST", url: "optIn.php", data: dataString, success: function() { $('.emailform').fadeOut(100).hide(); $('.simple-success').fadeIn(100).show(); alert('sucess') } }); return false; }); });

The script is pulling in the data from the form as I am able to view the data in an alert of the datastring.However for some odd reason the data never gets sent on to the database even though the PHP script seems correct. The PHP script is given below

<?php $firstname = stripslashes(strip_tags($_POST['firstname'])); $lastname = stripslashes(strip_tags($_POST['lastname'])); $email = stripslashes(strip_tags($_POST['email'])); $title = stripslashes(strip_tags($_POST['title'])); $organization = stripslashes(strip_tags($_POST['organization'])); $pdbHost = "localhost"; $pdbUserName = "******"; $pdbPassword = "******"; $pdbName = "db1080824_emails"; // Connect to mySQL $conlink = mysql_connect($pdbHost, $pdbUserName, $pdbPassword); if(!$conlink) {die('Unable to connect to '.$pdbHost);} if (!mysql_select_db($pdbName, $conlink)){die('Cannot find database '.$pdbName);} //SQL query $SQL2="INSERT INTO `db1080824_emails`.`emails` (`record_id` ,`firstname`,`lastname`,`email`,`title`,`organization`)VALUES (NULL , '".$firstname."', '".$lastname."', '".$email."', '".$title."', '".$organization."')"; mysql_query($SQL2); // Connect to Closing the connection mysql_close($conlink); ?>

The mysql server is located on the server and the database with tables has been created,so I am not sure whats going wrong here

Any inputs would be great

Answer1:

You can echo everything you need in php and alert it like this:

success: function(html) { $('.emailform').fadeOut(100).hide(); $('.simple-success').fadeIn(100).show(); alert(html) }

It will also show errors occured in php file.

Answer2:

First option: Use xdebug. <a href="http://xdebug.org" rel="nofollow">http://xdebug.org</a> This is the best option, as it will give you full debugging support and can help you fix a variety of PHP/MySQL problems. However, it can be a pain to get set up right, and depending on your environment it may not be an option.

Second option: View the PHP error log. The location of this log depends on your server setup. If you're using a hosting service, this should be available through the host's control panel. If your server is Linux with Apache, the log will usually be either in your Apache log file (/var/log/apache2/error.log or similar) or a vhost log file (a logs folder in the same location as your document root). Many PHP errors can be quickly diagnosed and fixed by just checking the log.

Third option: Print an error string. Try this:

if( !mysql_query( $SQL2 ) ) { die( 'Error ' . mysql_errno( ) . ': ' . mysql_error( ) ); }

Then check the response in Firebug or a similar tool.

And whatever you do, make sure you sanitize those inputs. Never assume any data coming from a web client is safe, because it's trivial to POST arbitrary data to a form. Try this: <a href="http://us.php.net/manual/en/function.mysql-real-escape-string.php" rel="nofollow">http://us.php.net/manual/en/function.mysql-real-escape-string.php</a>

Recommend

  • Alternative for django.db.close_connection()
  • Indexing k-d tree?
  • MySQL diacritic insensitive search (Arabic)
  • MySQL workbench : How to export mysql database to .sql file?
  • Changing MySQL charset to UTF8 on linux so it will work with JDBC
  • how to handle large size of update query in mysql with laravel
  • Codeigniter mysqli::real_connect() error
  • Java Date Format Conversion
  • Is need install mysql on AWS EC2 if i am using AWS rds as database instance?
  • Can't close mysql connections in R
  • What is the best data type to store boolean values in a database
  • Index Multiple Columns w/ Ruby on Rails
  • How to obtain service credentials for a service instance created on IBM Bluemix without binding the
  • Yii: any way to save the images in compressed form?
  • Hibernate generation index and foreign key with same name for MySQL
  • Query with in a loop getting slower and slower
  • Laravel Eloquent integration in Codeigniter
  • mysql select inside limit
  • Trouble connecting to Google Cloud SQL server from deployed app
  • JPA/Hibernate - Entity name seems to be important. If I rename to “Bob” works fine
  • Encode string to match encoded form field name in PHP POST array
  • Passing parameter through “window.location.href”
  • array_search() in Session Array
  • How to check if a string contains any string of a column in MySQL and vice versa?
  • Getting syntax error in mysql-php. You have an error in your SQL syntax;
  • JSON - slashes not escaping
  • PHP multiple file uploads
  • CORS with socket.io
  • mave 3.2 not able to access local nexus instance return 502 code
  • PDO error when wrong host name
  • MYSQ & MVC3 SQL connection error \\ ProviderManifestToken but I am using MySQL
  • Dynamically switching connect in Modelica
  • Tomcat memory Leak
  • nonblocking BIO_do_connect blocked when there is no internet connected
  • Redux Form - Not able to type anything in input
  • Get history of file changes from TFS to implement custom “blame”-behaviour of exceptions
  • Moving mysql files across servers
  • Cannot resolve symbol 'MyApi'
  • How to check if every primary key value is being referenced as foreign key in another table
  • Comma separated Values