Jump to content
Larry Ullman's Book Forums

How To Use Javascript Function Into Php Script?


Recommended Posts

How to use javascript function into php script? I am trying to change input language of the form as follow:

If session language id is gujarati, then I want to use the javascript function. But my form does not show submit button, or it seems script does not run after checking for language.

 

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

 

<meta name="copyright" content="Copyright © Vishal Monpara">

<script type="text/javascript" src="js/pramukhlib.js"></script>

<script language="javascript">

 

// Main object for phonetic processing

var pph;

 

</script>

</head>

 

<body>

 

<?php #script 2.5 - Add_book.php

 

//This script allows to add a new book detail into the database.

 

$page_title = 'Add book details';

 

require_once('../includes/configr.inc.php');

 

include('../includes/head.html');

 

require_once(MYSQL);

 

//Only display this form if the user is logged in:

 

echo '<h1>Welcome';

 

if(isset($_SESSION['first_name']))

{

echo ", {$_SESSION['first_name']}!";

}

 

echo '<h1>Add your book details.</h1>';

 

//Language specific words:

$t1 = $words['book_title'];

$a1 = $words['author'];

$r1 = $words['book_review'];

 

$l = $_SESSION['lid'];

$u = $_SESSION['user_id'];

 

 

 

if(isset($_POST['submit']) && isset($_SESSION['user_id']))

{ //Handle the form:

 

//Trim all the incoming data:

$trimmed = array_map('trim', $_POST);

 

//Assume invalid values:

$t = $a = $r = FALSE;

 

//check for a title:

 

if(!empty($_POST['title']))

{

$t = mysqli_real_escape_string($dbc, $trimmed['title']);

}

else {

echo '<p class="error">Please enter the book title.</p>';

}

 

//check for an author name:

if(!empty($_POST['author']))

{

$a = mysqli_real_escape_string($dbc, $trimmed['author']);

}

else {

echo '<p class="error">Please enter author name.</p>';

}

 

 

$r = mysqli_real_escape_string($dbc, trim($_POST['review']));

 

// echo $t ;

if($t && $a)

{ //If all is OK..

 

$q = "INSERT INTO books(lang_id, user_id,title, author, review, date) VALUES ($l, $u, '$t', '$a', '$r', now())";

 

$r = mysqli_query($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));

 

if(mysqli_affected_rows($dbc) == 1)

{

//If it ran OK..

 

echo 'The book is added.';

}

}

}

mysqli_close($dbc);

 

?>

 

<form action="addbook.php" method="post" accept-charset="utf-8">

<p><?php echo $t1; ?><input type="text" id="title" name="title" size="80" value="<?php if(isset($_POST['title'])) echo $_POST['title']; ?>" /></p>

<p><?php echo $a1; ?>:<input type="text" id="author" name="author" size="80" value="<?php if(isset($_POST['author'])) echo $_POST['author'] ?>" /></p>

 

<p><?php echo $r1; ?>:<textarea id="review" name="review" rows="20" cols="50" value="<?php if(isset($_POST['type'])) echo $_POST['type']; ?>" /></textarea></p>

 

 

 

<?php

//Check for language gujarati:

 

 

//If language is Gujarati then call Javascript function.

 

if($_SESSION['lid'] == 'Gujarati')

{

echo 'Gujarati';

//If it ran ok.

?>

<script language="javascript" type="text/javascript">

 

pph = new PramukhPhoneticHandler();

pph.convertToIndicIME("title", document.getElementById('title'),'gujarati');

pph.convertToIndicIME("author", document.getElementById('author'),'gujarati');

pph.convertToIndicIME("review", document.getElementById('review'),'gujarati');

 

</script>

 

<p><input type="submit" name="submit" value="Add" /></p>

<p><input type="hidden" name="submitted" value="TRUE" /></p>

 

 

<?php

 

} //End of if gujarati check.

 

?>

Link to comment
Share on other sites

The one-word answer is "Ajax".

 

Starting from the JavaScript side, you need to use Ajax to send a request to your PHP script, check the session ID from the PHP script, send an appropriate response back to the Javascript from the PHP script, and then use JavaScript to adjust the form accordingly.

 

The better thing to do though would be to create an appropriate cookie using only JavaScript from the start, and then you can eliminate PHP from this part of the process altogether (and you won't have to worry about learning Ajax either). For more information, please see the following:

 

http://www.w3schools.com/js/js_cookies.asp

 

If you need more help, please let us know.

Link to comment
Share on other sites

HartleySan

 

Thank you very much. But, I was trying to figure out how to do that and I changed my php script as follows. And it works.. Now, I am a beginner to PHP, I don't know javascript or Ajax. So, I tried finding a tutorial to pass php variable to javascript variable and used it in my script. It's working fine on my computer. It means that if a user has selected gujarati language, then he can type in gujarati fonts, otherwise he can type in English.

I wonder, will my script really work over the internet? It is working fine in my browser windows.

 

Here I have pasted only that part of the script which checks the session variable and then calls js function.

 

<?php

//Check for language gujarati:

//If language is Gujarati then call Javascript function.

 

if($_SESSION['lid'] == 11)

{

$lang = 11;

//If it ran ok.

// echo $lang;

?>

<script language="javascript" type="text/javascript">

jslid = "<?php echo $lang; ?>";

if (jslid == 11)

{

document.write(jslid);

pph = new PramukhPhoneticHandler();

pph.convertToIndicIME("title", document.getElementById('title'),'gujarati');

pph.convertToIndicIME("author", document.getElementById('author'),'gujarati');

pph.convertToIndicIME("review", document.getElementById('review'),'gujarati');

}

</script>

<?php

 

} //End of if gujarati check.

else {

//echo "other than gujarati ";

//echo $_SESSION['lid'];

}

?>

<p><input type="submit" name="submit" value="Add" /></p>

<p><input type="hidden" name="submitted" value="TRUE" /></p>

Link to comment
Share on other sites

To answer your question, yes, you can use PHP to literally write JavaScript between script tags. That is definitely doable, as you have proven, and if it works in your browser, it should work fine on the Net as well.

 

With that said, what you have done is not the preferable method. However, if it makes sense to you and it works, then there is no reason to worry about it, I suppose.

Link to comment
Share on other sites

  • 2 weeks later...

Well, Now I can understand what you said. My script sometimes run well and some other time does not work the way it should. So I definitely need to change it to javascript. So I was looking at js cookies section. Now my problem is this. I am using php session from the start of the website, where it sets userid and his language. Now I don't understand how can I use this values to get them work in the js cookies.

The user is looking the site in his language, and for this one page how can I ask again to choose his language? The page should detect the language. Please guide me.

 

thanks

Link to comment
Share on other sites

There are three ways I can think of doing this:

 

1) Use the method I think you used before, which involves using PHP to place values in JavaScript variables. I find this to be the most broke-ass (i.e., the worst) method. Nevertheless, it works. An example of this can be seen in the following:

 

http://p2p.wrox.com/php-faqs/11606-q-how-do-i-pass-php-variables-javascript.html

 

2) Use Ajax on page load to send a request from the JavaScript to the PHP to get the session information, and then send that information back to JavaScript for use. This is the most elegant, but probably the most difficult. Larry wrote a great book about Ajax, and I highly recommend it. Ajax is a bit tricky, but it's a technology that's here to stay and very useful. Instead of fighting it, I encourage that you try to learn it.

 

3) A third option would be to use DHTML (which essentially means using JavaScript together with the HTML DOM) to reference the language value on the page, and set that to the JS cookie. For example:

 

...

<div id="currLang">Arabic</div>

<script>

 alert(document.getElementById('currLang').innerHTML); // Alerts "Arabic".

</script>

...

 

Anyway, go whatever route you want. Hopefully one of them works for you.

 

Edit: A lot of people use jQuery these days for handling Ajax. You may want to consider doing that.

Link to comment
Share on other sites

'HartleySan'

 

Thanks a lot for your detailed reply.

For the following method, I got idea from this link: http://devzone.zend.com/article/1300#comment_form

 

 

1) Use the method I think you used before, which involves using PHP to place values in JavaScript variables. I find this to be the most broke-ass (i.e., the worst) method. Nevertheless, it works. An example of this can be seen in the following:

 

 

I wish I could learn all the technologies required to make a website, but right now I'm stuck with php. This particular part is not working today, as it was working a day before.

Any how, thanks a lot again..

Link to comment
Share on other sites

Hehe! I know how you feel. Sometimes, while illogical, it does seem like a website works fine one day, and the next day (without changing anything), suddenly it doesn't. Mysterious, mysterious thing, eh?

 

Anyway, of course it would be ideal if we could all know everything about all the technologies out there, but real life doesn't allow for such things. I have spent the past couple years of my life wrapping my head around Ajax, and finally, I think I get it pretty well, so I understand what you mean.

 

For the time being, I say go with what works, and when you have the time, push yourself to learn more and refine your techniques.

 

Larry's Ajax book is really good too!

Link to comment
Share on other sites

Well, it feels good to read your comment and suggestion. I am taking your advice literally to move forward one at a time, and will keep learning.

 

Now there is one new problem. I wanted to check form input values with regular expressions. They work fine with English language, but not with Gujarati. I'm using one simple regular expression as Larry has shown to check a name in the registration script.

Link to comment
Share on other sites

The best way to check non-English characters with regexes is to use Unicode code points/blocks.

 

I can't remember exactly where, but I answered a similar question from someone trying to check French names recently. I believe the post was on the e-commerce board. Let me find the post, and I'll link it here.

 

Edit: The post was on this board. Anyway, here's the link:

 

http://www.larryullman.com/forums/index.php?/topic/365-pregmatch/

 

Hope that helps.

Link to comment
Share on other sites

 Share

×
×
  • Create New...