Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
| Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old January 4th, 2005, 02:03 PM
Registered User
 
Join Date: Jan 2005
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Validating submitted data

On my website, I got a script with which users can react on certain things. They also have to include their e-mail adresses. Because a lot of visitors do not put in a valid e-mail address, or nothing at all, I want to validate it with JavaScript before it is stored in my SQL database using PHP. But, when you submit data from a form, you can't read it with Javascript, neither can you make variables which can be read by PHP. How can this be solved?

  #2 (permalink)  
Old January 4th, 2005, 02:05 PM
Registered User
 
Join Date: Jan 2005
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

But correct me if I'm wrong about the forms with JavaScript and PHP... I'm just a beginner :D

  #3 (permalink)  
Old January 4th, 2005, 02:54 PM
Registered User
 
Join Date: Jan 2005
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hmm, sorted it out myself. You can actually store a SQL query in a Javascript variable and just display the variable. So the inputs are first checked, put into variables, and then inputted in the DB using PHP, something like this:

Code:
<?php
// When submit button is pressed
if(isset($sbm)==1) {
?>
<script type="text/javascript">
// Read names and e-mail address from POSTED form
var name = "<?php echo $_POST['name']; ?>"
var email = "<?php echo $_POST['email']; ?>"

// Set the mysql query in var query
var query = '<?php echo "INSERT INTO names (name, email) VALUES ("$' + name + ', $' + email + ')";'

// Connect to MySQL
document.write('<?php echo "\$db = mysql_connect(\"\", \"\", \"\")/r"; ?>')
document.write('<?php echo "\$query = ' + query + '/r";')
document.write('<?php echo "mysql_query($query)";')
document.write('<?php echo "mysql_close()";')
</script>
<?php
}
?>
Would this work theoretically? Maybe there are some mistakes in the /-es and ' or ", but could this work?

  #4 (permalink)  
Old January 4th, 2005, 03:03 PM
Registered User
 
Join Date: Jan 2005
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

There are some more mistakes, but it's just the idea...

  #5 (permalink)  
Old January 4th, 2005, 03:41 PM
Registered User
 
Join Date: Jan 2005
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here some updated code, which doesn't give any errors, but it also doesn't seem to work. No values are put in my local db...

[code]
<?php
// When submit button is pressed
if(isset($sbm)==1) {
?>
<script type="text/javascript">
// Read names and e-mail address from POSTED form
naam = "<?php echo $_POST['name']; ?>"
email = "<?php echo $_POST['email']; ?>"

// Connect to MySQL
document.write('<?php echo "<?php \$db = mysql_connect(\"localhost\", \"jeugdavanti\", \"stifmeister\") or die (\"Vautje!\");')
document.write('mysql_select_db(\"avanti\", \$db);')
document.write('\$query = \"INSERT INTO namen (naam, email) VALUES (\"', naam, '\", \"', email, '\")\";')
document.write('mysql_query(\$query);')
document.write('mysql_close(); ?>"; ?>')
document.write('Het is gelukt!')
</script>
<?php
} else {
?>
<form method="post" action="db.php" name="formulier">
<input name="name"> Je naam<br>
<input name="email"> Je email<br>
<input type="submit" name="sbm" value="Invoegen"></form>
<?php
}
?>[code]

Some variables and other stuff are named in Dutch... but that shouldn't be a problem for you guys...

Any solutions? Try the script if you want, it just displays the confirmation ("Het is gelukt!") but nothing is inserted into the db. Maybe it is something with the variables

  #6 (permalink)  
Old January 4th, 2005, 11:37 PM
Authorized User
 
Join Date: Jan 2005
Location: , , .
Posts: 82
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I think you are making things alot harder than they should be.


try phpfreaks.com tutorials. Zend.com also has good tutorials on what you are looking for...

but for now, try writing the php code without outputing it from javascript, unless thats what you specificly want.

Javascript being the client-side scrip language does not work well if you embed server side script (php) in it.




  #7 (permalink)  
Old January 5th, 2005, 06:18 AM
Registered User
 
Join Date: Jan 2005
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok, thanks...



Similar Threads
Thread Thread Starter Forum Replies Last Post
pre checking of submitted data bond007 PHP How-To 1 October 15th, 2008 11:24 PM
How do I clear a form after it has been submitted? u2clone ASP.NET 2.0 Basics 7 February 12th, 2008 04:00 PM
Chapter 8: Validating the Format of Data rubyjews BOOK: Beginning Ruby on Rails 2 March 15th, 2007 08:26 PM
Page gets submitted automatically! recruitsen Classic ASP Basics 1 September 22nd, 2004 05:15 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.