/    Sign up×
Community /Pin to ProfileBookmark

taking over value from Javascript.

Hey everybody!

I am trying to set up a language qualification test for a language school in Javascript, which works quite fine,

but now I would like to create another input box for the name of the student and have his name & number of correctly answered questions

(value=totalquestions-incorrect in [URL=”http://testdenivel.110mb.com/results.htm”]http://testdenivel.110mb.com/results.htm[/URL])

sent to an email address. Could anybody help me with some lines of code?

Your help is greatly appreciated,

best regards,

bob

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@SrWebDeveloperMay 17.2010 — All that requires server side processing, i.e. creating an HTML form which calls a PHP script which then calculates the value you want in a generated results page. As this is a PHP forum, you'd do this in PHP code, naturally. Your question is vague -- we're not going to code it for you 100%, so if you're new to PHP learn how to use it, get something started and we'll gladly help you debug if you get into trouble (report all errors and line numbers if so). Or seek the help of a pro.

But learning forms processing in PHP isn't hard, i.e. this is an excellent tutorial on that plus another for email too:

Lessons on basic PHP forms processing

Tutorial on basic E-Mail, PHP and forms

Both easy to learn!

-jim
Copy linkTweet thisAlerts:
@estudisantsauthorMay 17.2010 — Thanks for your quick answer. I guess I expressed myself a wee bit fuzzy, obviously I don't want to ask you to code the solution for my problem.

I am aware that what I want is server-sided, that's why I posted it in the php-forum.

As I have no deep understanding of php, I thought somebody could give me a hint from where to start.

So:

I have my quiz result page (HTML and JS):

[CODE]</head><body style="background-color: rgb(255, 255, 255);">
<p align="center"><strong><font face="Arial">
<script src="quizconfig.js">
</script><big>RESULTADO</big></font></strong></p>

<div align="center">
<center>
<table border="0" cellpadding="0" cellspacing="0">
<tbody>

<tr>
<td width="100%">
<form method="post" name="result">
<table border="0" cellpadding="0" height="116" width="100%">
<tbody>
<tr>
<td bgcolor="#d3ffa8" height="25"><strong><font face="Arial">respuestas correctas<br>
</font></strong></td>

<td height="25">
<p><input name="p" size="24" type="text"></p>
</td>
</tr>
<tr>
<td bgcolor="#d3ffa8" height="17"><strong><font face="Arial"><br>
</font></strong></td>
<td height="17">
<p><textarea name="T2" rows="3" cols="24" wrap="virtual"></textarea></p>

</td>
</tr>
<tr>
<td bgcolor="#d3ffa8" height="25"><strong><font face="Arial">porcentaje:</font></strong></td>
<td height="25"><input name="q" size="8" type="text"></td>
</tr>
</tbody>
</table>

</form>
</td>
</tr>
</tbody>
</table>
</center>
</div>

<form method="post">
<div align="center">
<center>

<p>
<script>
var wrong=0
for (e=0;e<=2;e++)
document.result[e].value=""

var results=document.cookie.split(";")
for (n=0;n<=results.length-1;n++){
if (results[n].charAt(1)=='q')
parse=n

}

var incorrect=results[parse].split("=")
incorrect=incorrect[1].split("/")
if (incorrect[incorrect.length-1]=='b')
incorrect=""
document.result[0].value=totalquestions-incorrect.length+" out of "+totalquestions
document.result[2].value=(totalquestions-incorrect.length)/totalquestions*100+"%"
for (temp=0;temp<incorrect.length;temp++)
document.result[1].value+=incorrect[temp]+", "


</script><input value="Otra vez!" name="B1" onclick="history.go(-1)" type="button"> </p>
</center>
</div>
</form>

<p id="footnote" align="center"><font face="arial" size="-1">Quiz
script provided by<br>


</body></html>[/CODE]


and I am trying to write some lines of php, which take the result value from JS and send them, along with the name, as an email.

I think I understand the problem of javascript happening client- and php server-sided. So how do I make that bridge?

Would writing a cookie in JS and then reading it out in php be an option?

best regards,

bob
Copy linkTweet thisAlerts:
@SrWebDeveloperMay 17.2010 — Okay. Two things to remember...

1) In any HTML form, there is an action argument. Use method post and set action to a PHP script i.e. "results.php". That's the bridge. All form elements end up in $_POST['keyname'] in PHP where keyname is the name of the each form element when submitted, i.e. <input type='text' name='foobar'> becomes $_POST['foobar']. Process accordingly in results.php.

2) Use PHP to generate the Javascript if you wish to embed all of it in a form and/or if you embed data in a PHP variable in that form. Could be as simple as:

[code=php]print '<script> var foobar='$foobar'; </script>'; // demonstrating that you use PHP to generate Javascript plus this is how you embed PHP variables in Javascript.
[/code]


If you use Javascript to directly manage the form before submission [I]in any way[/I], no problem, just note that when the form is submitted the data still ends up in $_POST['keyname'] as I noted earlier. Remember the golden rule, if it can be done server side, do it that way also, even if it can be done via Javascript. Never *rely* on Javascript for anything "if possible."

I again advise followingthe tutorial links to code it for real, now that I've explained the high level conceptual view. Enjoy and have fun!

-jim
Copy linkTweet thisAlerts:
@SrWebDeveloperMay 17.2010 — As to cookies, so long as you create it properly it can be read between each language. This implies if it's non-session then make sure the proper path is set and you might want to JSON encode if it's a complex data type. Creating cookies in native JS is certainly easy enough. I personally recommend using jQuery to do it, it's a framework you can download which simplifies the process and does an excellent job conforming to standards with one simple line of code to get it done.

Here is help on that with respect to jQuery/JS cookies and PHP:

http://tenseveninteractive.com/content/javascript-cookies-with-php

my .02

-jim
Copy linkTweet thisAlerts:
@estudisantsauthorMay 18.2010 — wow, really big thanks for all that useful information, I'll read both tuts and give it a try.

cheers,

bob
×

Success!

Help @estudisants spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...