 |
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
|
|
|

January 4th, 2005, 02:03 PM
|
Registered User
|
|
Join Date: Jan 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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?
|

January 4th, 2005, 02:05 PM
|
Registered User
|
|
Join Date: Jan 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
But correct me if I'm wrong about the forms with JavaScript and PHP... I'm just a beginner :D
|

January 4th, 2005, 02:54 PM
|
Registered User
|
|
Join Date: Jan 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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?
|

January 4th, 2005, 03:03 PM
|
Registered User
|
|
Join Date: Jan 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
There are some more mistakes, but it's just the idea...
|

January 4th, 2005, 03:41 PM
|
Registered User
|
|
Join Date: Jan 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

January 4th, 2005, 11:37 PM
|
Authorized User
|
|
Join Date: Jan 2005
Posts: 82
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|

January 5th, 2005, 06:18 AM
|
Registered User
|
|
Join Date: Jan 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Ok, thanks...
|
|
 |