PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 15th, 2013, 05:08 PM
Squigglyo Squigglyo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 9 Squigglyo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 46 m 40 sec
Reputation Power: 0
Need help - Javascript from Php

Hello all

In my webpage, I have a contact form. When the done button is clicked, it calls a Javascript function that changes the values of the text boxes to reflect the neccesary changes and show warnings.

Code:
var nameStr = safe_string(document.getElementById('cf_name').value);
			var emailStr = safe_string(document.getElementById('cf_email').value);
			var messageStr = safe_string(document.getElementById('cf_message').value);
			
			if(nameStr == "")
				document.getElementById('cf_nameCheck').value = "*Enter your name*";
			else
				document.getElementById('cf_nameCheck').value = "";
			
			if(emailStr == "" || emailStr.indexOf("@") == -1 || emailStr.indexOf(".") == -1)
				document.getElementById('cf_emailCheck').value = "*Enter a valid email*";
			else
				document.getElementById('cf_emailCheck').value = "";
				
			if(messageStr == "")
				document.getElementById('cf_messageCheck').value = "*Enter your message*";
			else
				document.getElementById('cf_messageCheck').value = "";	
			
			
			document.getElementById('cf_name').value = nameStr;
			document.getElementById('cf_email').value = emailStr;
			document.getElementById('cf_message').value = messageStr;
			
			
			
			if(document.getElementById('cf_nameCheck').value == "")
				if(document.getElementById('cf_emailCheck').value == "")
					if(document.getElementById('cf_messageCheck').value == "")
						open_page('#Email_Sent');
			
			return false;

this is located within a javascript file and shows the user what they did wrong.
safe_string is a function that strips out html from input.

All this is fine, but not when I want to check if the captcha is fine, client side.

In the contact submission, using php, this is the end result of the file.
PHP Code:
include_once $_SERVER['DOCUMENT_ROOT'] . '/securimage/securimage.php';

$securimage = new Securimage();
if (
$securimage->check($_POST['captcha_code']))
                    {
                        
$mail_status mail($mail_to$subject$body_message$headers);
                    } 


it checks if the given captcha is correct.



My problem is, that I cannot find a way to do the Javascript bit, (change the value of the text input) through Php, and cannot figure out how to check the captcha in the javascript.



Either solution fixes my problem, and I have spent a good 3 days researching and am still no closer.

Any help is greatly appreciated.

Reply With Quote
  #2  
Old January 15th, 2013, 05:51 PM
requinix's Avatar
requinix requinix is offline
Still alive
Dev Shed God 16th Plane (12500 - 12999 posts)
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,860 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 5 Days 5 h 30 m 31 sec
Reputation Power: 8977
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
Don't use Javascript to check the CAPTCHA: it'll give bots the opportunity to easily defeat it. It needs to be checked in PHP.

Make the form support getting default values. As in your PHP looks like
PHP Code:
 $name /* value from form, or empty string if not submitted */

Code:
<input type="name" value="<?php echo htmlentities($name); ?>" />

Then if the CAPTCHA check fails you redisplay the form.

Protip: you should make your PHP support the same verification that the Javascript does, otherwise a malicious user could simply disable Javascript in their browser and fill out the form however they wanted.

Reply With Quote
  #3  
Old January 15th, 2013, 07:50 PM
Squigglyo Squigglyo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 9 Squigglyo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 46 m 40 sec
Reputation Power: 0
Quote:
Originally Posted by requinix

PHP Code:
 $name /* value from form, or empty string if not submitted */

Code:
<input type="name" value="<?php echo htmlentities($name); ?>" />




Does this require the page to be reloaded? Because at the moment, my entire site and all of its 'pages' exist on the index page and a reload defeats the purpose of my design.

Quote:
Originally Posted by requinix

Protip: you should make your PHP support the same verification that the Javascript does, otherwise a malicious user could simply disable Javascript in their browser and fill out the form however they wanted.


In my Php, I take the values of the relevant text fields and put them into strings. I then strip them the same way I do in the Javascript and check for nulls.
The Php is the part that checks and passes the values onto the email. The Javascript is the part that simply checks and changes text fields and warning labels.

Reply With Quote
  #4  
Old January 15th, 2013, 08:25 PM
Squigglyo Squigglyo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 9 Squigglyo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 46 m 40 sec
Reputation Power: 0
Looking at the current code I have...

Code:
<a href="#" onclick="document.getElementById('captcha').src = '/securimage/securimage_show.php?' + Math.random(); return false">[ Change Code ]</a>

This line of code can reload the captcha image, and it does not reload the page.


So is there a way that I can call some sort of Php function that returns a true/false that will allow me to do a
Code:
document.getElementById.value = "*Invalid*"	

when this link is clicked?
Code:
<a href ="#!/CheckForm" class="button1" onclick="$(this).closest('form').submit()"onmouseover="" style="cursor: pointer;"><span></span><strong>Send</strong></a>

Reply With Quote
  #5  
Old January 15th, 2013, 08:34 PM
requinix's Avatar
requinix requinix is offline
Still alive
Dev Shed God 16th Plane (12500 - 12999 posts)
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,860 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 5 Days 5 h 30 m 31 sec
Reputation Power: 8977
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
Yes: AJAX.

You do an AJAX request with the value the user entered to some PHP script which signals (output, return status code, whatever) whether it's correct or not.

Reply With Quote
  #6  
Old January 15th, 2013, 08:36 PM
portcitysoftwar portcitysoftwar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 163 portcitysoftwar User rank is Sergeant (500 - 2000 Reputation Level)portcitysoftwar User rank is Sergeant (500 - 2000 Reputation Level)portcitysoftwar User rank is Sergeant (500 - 2000 Reputation Level)portcitysoftwar User rank is Sergeant (500 - 2000 Reputation Level)portcitysoftwar User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 13 h 18 m 54 sec
Reputation Power: 17
Doing checks and processing on the local browser is a horrible idea. You do not want the value of the captcha within the html document because a bot will have access to the data. If you don't want to reload the frame then use AJAX to request a php page with the form data and then get XML confirmation from the server. Use javascript at that point to change the page.

Quote:
Originally Posted by Squigglyo
Looking at the current code I have...

Code:
<a href="#" onclick="document.getElementById('captcha').src = '/securimage/securimage_show.php?' + Math.random(); return false">[ Change Code ]</a>

This line of code can reload the captcha image, and it does not reload the page.


So is there a way that I can call some sort of Php function that returns a true/false that will allow me to do a
Code:
document.getElementById.value = "*Invalid*"	

when this link is clicked?
Code:
<a href ="#!/CheckForm" class="button1" onclick="$(this).closest('form').submit()"onmouseover="" style="cursor: pointer;"><span></span><strong>Send</strong></a>

Reply With Quote
  #7  
Old January 15th, 2013, 10:04 PM
requinix's Avatar
requinix requinix is offline
Still alive
Dev Shed God 16th Plane (12500 - 12999 posts)
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,860 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 5 Days 5 h 30 m 31 sec
Reputation Power: 8977
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
Quote:
Originally Posted by portcitysoftwar
Doing checks and processing on the local browser is a horrible idea. You do not want the value of the captcha within the html document because a bot will have access to the data. If you don't want to reload the frame then use AJAX to request a php page with the form data and then get XML confirmation from the server. Use javascript at that point to change the page.

There's nothing wrong with doing validation on the client so long as it's done on the server too. As for CAPTCHA, naturally you wouldn't want to send the correct value to the client in any way, shape, or form... besides the image... but it's fine to send the client's input back to the server as AJAX.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > Ned help - Javascript from Php

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap