...

View Full Version : Javascript return value in PHP variable



Pattatj
09-13-2011, 10:07 AM
What I wanted to do is to call a Javascript function and put the return value into a php variable, is this possible?

Like this:



echo '<script type="text/javascript">';
$val = 1;
$agree = 'confirm("Validity: ' . $val . '\nAre you sure?");';

if($agree) {
...
} else {
...
}
echo '</script>';

Thanks for your help and time!
'x.

Pattatj
09-13-2011, 12:06 PM
I've asked around and the only solution is by sending the data from a javascript function and get or post it from the PHP script. I'll post my solution here later when I've implemented it.

'x.

BluePanther
09-13-2011, 09:03 PM
Your only solution is to use a POST or GET send to the php script in question. Javascript is client side, PHP is server side and they're separate for a good reason.
What you're doing can be achieved in different ways, including a 'splash' page in the php itself, or another way in Javascript which on confirm, redirects to a page, on false it stays on the current.

Pattatj
09-14-2011, 04:56 PM
Ok, what I've done is basically taking option 2 from BluePanther :)
Here it is:

page1.php:


<head>
<script type="text/javascript">
function validateForm() {
if(document.forms["addForm"].cmp_val.value == "") {
alert("Validity can't be empty. Please select a validity.");
return false;
}
return true;
}

function sendData() {
if(validateForm()) {
var val = document.getElementById("cmp_val").value;
var sup = document.getElementById("cmp_sup").value;
var agree = confirm("Validity: " + val + "\nSupplier: " + sup + "\n\nAre you sure the information is correct and that you want to insert it into the database?");

if(agree) {
window.location.href = "page2.php?cmp_val=" + val + "&cmp_sup=" + sup;
}
}
}
</script>
</head>
<body>
<form name="addForm" onsubmit="return false;">
<table>
<th>Component</th>
<tr>
<td>Validity: </td>
<td>
<select name="cmp_val" id="cmp_val">
<option value="">Choose a validity</option>
<option value="Cancelled">Cancelled</option>
<option value="Obsolete">Obsolete</option>
<option value="Pending">Pending</option>
<option value="Valid">Valid</option>
<option value="Expelled">Expelled</option>
</select>
</td>
</tr>
<tr>
<td>Supplier: </td>
<td><input id="cmp_sup" class="textfield" type="text" name="cmp_sup" /></td>
</tr>
<tr>
<td><input type="button" value="Cancel" onclick="window.location.href='homepage.html'" style="cursor: pointer;" /></td>
<td><input class="input" type="submit" value="Add" onclick="sendData()" style="cursor: pointer;" /></td>
</tr>

</table>
</form>
</body>


homepage can be anything from index.html to whatever you use as your homepage.
Next is page 2, the page that will do everything that has to be done when the form is submitted.

page2.php:


$val = $_GET['cmp_val'];
$sup = $_GET['cmp_sup'];

The rest of page2.php is up to you, what I did was checking the form input some more, store the data in the database, an automatic redirect back to the homepage and giving a signal to the homepage so that the homepage will show the data the user just entered.

Automatic redirect:


echo '<script type="text/javascript">';
echo 'setTimeout("window.location.href=\'homepage.php\'", 3000);';
echo '</script>';

The sign is a session variable I set like this:


$_SESSION['directSearch'] = $sup;

And retrieve like this:


<script type="text/javascript">
function init() {
<?php
if(isset($_SESSION['directSearch'])) {
// Do something with the passed variable (in my case the supplier)
}
<?php
}

window.onload = init;
</script>

I did it in like half an hour from scratch, so it's very easy :)
Hope I helped anyone besides just me :P
Enjoy!

'x.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum