...

View Full Version : passing values from javascript to php



meshi
08-31-2007, 03:20 PM
hi everyone..please help me with my code
CODE:
<script language="javascript">
function tf()
{
if (actiontype.options[actiontype.selectedIndex].value == "1" || actiontype.options[actiontype.selectedIndex].value == "2" || actiontype.options[actiontype.selectedIndex].value == "8" || actiontype.options[actiontype.selectedIndex].value == "15")
var txt='true';
else
var txt='false';
}
<script>
<?php
if (tf()=='true')
{
echo'<tr>
<td class="table-list-title" id="s">Interview Schedule</td>
</tr>';
}
?>
i cant pass the value of txt..please help me how to pass it..:)

Nightfire
08-31-2007, 03:24 PM
PHP is parsed before client scripting, including JS. So you can't transfer JS var's to PHP.

Mwnciau
08-31-2007, 03:25 PM
You can pass it in an URL:


window.location = 'index.php?txt=' + txt;

and then in PHP access it with


$_GET['txt']

meshi
08-31-2007, 03:37 PM
what i did is this

CODE:
<script language="javascript">
function tf()
{
if (actiontype.options[actiontype.selectedIndex].value == "1" || actiontype.options[actiontype.selectedIndex].value == "2" || actiontype.options[actiontype.selectedIndex].value == "8" || actiontype.options[actiontype.selectedIndex].value == "15")
var txt='true';
else
var txt='false';
window.location = 'menu.php'index.php?txt=' + txt;
}


i tried this one but it did not work..please correct my code.
if ($_GET[txt]==true)
echo '<td class="table-list-title" id="s">Interview Schedule</td>';

Inigoesdr
08-31-2007, 03:39 PM
window.location = 'menu.php'index.php?txt=' + txt;
You have an extra single-quote in there. It should be a forward-slash.


if ($_GET['txt'] == 'true')

meshi
08-31-2007, 03:47 PM
still it did'm work

meshi
08-31-2007, 03:49 PM
this is my edited code..still it did'nt work

<script language="javascript">
function tf()
{
if (actiontype.options[actiontype.selectedIndex].value == "1" || actiontype.options[actiontype.selectedIndex].value == "2" || actiontype.options[actiontype.selectedIndex].value == "8" || actiontype.options[actiontype.selectedIndex].value == "15")
var txt='true';
else
var txt='false';
window.location = 'menu.php'?txt=' + txt;
}
</script>
<?php
if ($_GET['txt']=='true')
echo '<td class="table-list-title" id="s">Interview Schedule</td>';
?>

Mwnciau
08-31-2007, 03:50 PM
<script language="javascript">
function tf()
{
if (actiontype.options[actiontype.selectedIndex].value == "1" || actiontype.options[actiontype.selectedIndex].value == "2" || actiontype.options[actiontype.selectedIndex].value == "8" || actiontype.options[actiontype.selectedIndex].value == "15")
var txt='true';
else
var txt='false';
if (txt != '<?php echo $_GET['txt']; ?>')
window.location = 'menu.php?txt=' + txt;
}
</script>
<?php
if ($_GET['txt']=='true')
echo '<td class="table-list-title" id="s">Interview Schedule</td>';
?>

meshi
08-31-2007, 03:53 PM
is it not need to call the function tf()..all i need to do is get the value of txt?

meshi
08-31-2007, 03:57 PM
still it didnt work

Mwnciau
08-31-2007, 04:10 PM
It works fine for me when I remove if (actiontype.options[actiontype.selectedIndex].value == "1" || actiontype.options[actiontype.selectedIndex].value == "2" || actiontype.options[actiontype.selectedIndex].value == "8" || actiontype.options[actiontype.selectedIndex].value == "15") , looks like you have a javascript error.

meshi
08-31-2007, 04:58 PM
i got ur point it seems it works now.tanks.is there any way that it would not load a page.i will ger the value by not loading the page?

Bahamut
08-31-2007, 05:19 PM
<script language="javascript">
function tf()
{
var actiontype = document.forms['actions']['eventcat'];

if (actiontype.options[actiontype.selectedIndex].value == "1")
{
document.all('div_one').style.display = "";
document.all('div_two').style.display = "none";
}
else if (actiontype.options[actiontype.selectedIndex].value == "2")
{
document.all('div_one').style.display = "none";
document.all('div_two').style.display = "";
} else {
document.all('div_one').style.display = "none";
document.all('div_two').style.display = "none";
}
}
</script>

<form name='actions' method='post'>
<select name='eventcat' size='1' onChange='tf()'>
<option value='0'>Select Type</option>
<option value='1'>One</option>
<option value='2'>Two</option>
</select>
<?php
echo "<div id='div_one' style='display:none;'>";
echo "<p>One 1 <input type='text' name='one1'>";
echo "<p>One 2 <input type='text' name='one2'>";
echo "<p>One 3 <input type='text' name='one3'>";
echo "<p>One 4 <input type='text' name='one4'>";
echo "<p>One 5 <input type='text' name='one5'>";
echo "</div>";

echo "<div id='div_two' style='display:none;'>";
echo "<p>Two 1 <input type='text' name='two1'>";
echo "<p>Two 2 <input type='text' name='two2'>";
echo "<p>Two 2 <input type='text' name='two3'>";
echo "<p>Two 3 <input type='text' name='two4'>";
echo "<p>Two 4 <input type='text' name='two5'>";
echo "</div>";
?>
</form>


???

che_anj
08-31-2007, 06:18 PM
Mechelle I told you Bahamut is the best. hehehhe

Nightfire
08-31-2007, 06:31 PM
There's no point in using php to echo out those divs etc if js and css is controlling them

meshi
09-03-2007, 02:52 PM
its not working

meshi
09-03-2007, 02:56 PM
i tried this one please correct my codes

<script language="javascript">
function tf()
{
var actiontype = document.forms['actions']['eventcat'];

if (actiontype.options[actiontype.selectedIndex].value == "1" || actiontype.options[actiontype.selectedIndex].value == "2" || actiontype.options[actiontype.selectedIndex].value == "8" || actiontype.options[actiontype.selectedIndex].value == "15")
{
document.all('div_one').style.display = "inline";
}
else if (actiontype.options[actiontype.selectedIndex].value == "0")
{
document.all('div_one').style.display = "none";
} else
{
document.all('div_one').style.display = "none";
}
}

</script>
<?php
echo '<td colspan="3">'.html_select("eventcat",0,array_of("eventcat"),"",$actype,"tf();").'
</td></tr>';
echo "<div id='div_one' style='display:none;'>";
echo '<tr>
<td class="table-list-title">Interview Schedule</td>
<td>
<input type="text" name="interviewdate" size="20" readonly value="'.date('d/m/Y', strtotime($rowa[interviewdate])).'" ><input type="reset" value=" .. " name="ambot"
onclick="return showCalendar(\'sel3\', \'%d/%m/%Y\');">&nbsp;
<input type="text" name="t" class="inputtime" value="Time:" border="0">
<input type="text" name="interviewtime" size="8" value="'.$rowa[interviewtime].'" >
</td>
</tr>
</div>';
?>


its still not working. please anyone can solve it.

Bahamut
09-03-2007, 03:57 PM
im not sure, if the div will work inside a tr? try <div><table></table></div>

meshi
09-03-2007, 04:15 PM
still its not working

meshi
09-03-2007, 04:49 PM
please bahamut if u have other idea.. help me with this problem.in stock with this.

Bahamut
09-03-2007, 05:15 PM
<script language="javascript">
function tf()
{
var actiontype = document.forms['actions']['eventcat'];

if (actiontype.options[actiontype.selectedIndex].value == "1")
{
document.all('div_one').style.display = "inline";
document.all('div_two').style.display = "none";
}
else if (actiontype.options[actiontype.selectedIndex].value == "2")
{
document.all('div_one').style.display = "none";
document.all('div_two').style.display = "inline";
}
else if (actiontype.options[actiontype.selectedIndex].value == "0")
{
document.all('div_one').style.display = "none";
document.all('div_two').style.display = "none";
}

}
</script>
<form name='actions' method='post'>
<select name='eventcat' size='1' onChange='tf()'>
<option value='0'>Select Type</option>
<option value='1'>One</option>
<option value='2'>Two</option>
</select>
<?php
//echo '<td colspan="3">'.html_select("eventcat",0,array_of("eventcat"),"",$actype,"tf();").'</td></tr>';
echo "<div id='div_one' style='display:none;'>";
echo '
<table><tr>
<td class="table-list-title">Interview Schedule</td>
<td>
<input type="text" name="interviewdate" size="20" readonly value="'.date('d/m/Y', strtotime($rowa[interviewdate])).'" ><input type="reset" value=" .. " name="ambot" onclick="return showCalendar(\'sel3\', \'%d/%m/%Y\');">&nbsp;
<input type="text" name="t" class="inputtime" value="Time:" border="0">
<input type="text" name="interviewtime" size="8" value="'.$rowa[interviewtime].'" >
</td>
</tr>
</table>
</div>';

echo "<div id='div_two' style='display:none;'>";
echo '
<table><tr>
<td class="table-list-title">Placement Schedule</td>
<td>
<input type="text" name="t" class="inputtime" value="Time:" border="0">
<input type="text" name="interviewtime" size="8" value="'.$rowa[interviewtime].'" >
<input type="text" name="interviewdate" size="20" readonly value="'.date('d/m/Y', strtotime($rowa[interviewdate])).'" ><input type="reset" value=" .. " name="ambot" onclick="return showCalendar(\'sel3\', \'%d/%m/%Y\');">&nbsp;
</td>
</tr>
</table>
</div>';
?>
</form>


hmm

meshi
09-03-2007, 05:32 PM
I really appreciate ur replies but it seems it does not work with my code.thak you for the effort.

CFMaBiSmAd
09-03-2007, 06:11 PM
There are three ways to get a value from a browser to a web server - 1) As a parameter on the end of the URL and then request that URL, 2) As data in a form and then that form is submitted, and 3) Using AJAX techniques in a JavaScript XMLHttpRequest object.

The first two methods cause the page to be refreshed, which if I am reading post #12 in this thread correctly, you don't want a page to be loaded/refreshed? If this is true, you would need to use AJAX techniques.

If you describe what it is you are trying to achieve, you can probably get a quicker solution, instead of everyone just posting code that sends values to the server, but does not necessarily do it in the way you need it to be done.

Also, when you write that something is not working, you need to describe what it is doing and how this differs from what you want it to be doing.

meshi
09-06-2007, 04:17 PM
i already get it.thanks coding forums for the idea

che_anj
09-06-2007, 04:30 PM
try this..



<script type="text/javascript">
function displayCities()
{
var actiontype = document.forms['actions']['eventcat'];
if (actiontype.options[actiontype.selectedIndex].value == "1" || actiontype.options[actiontype.selectedIndex].value == "2" || actiontype.options[actiontype.selectedIndex].value == "8" || actiontype.options[actiontype.selectedIndex].value == "15")
{
document.getElementById('div_one').style.display='';
}
else if (actiontype.options[actiontype.selectedIndex].value == "0")
{
document.getElementById('div_one').style.display='none';
}
else
{
document.getElementById('div_one').style.display='none';
}
}
function checkk()
{
if (document.actions.company.value.length < 1)
{
alert ("Please Enter Company Name");
return false;
}
}
</script>

in your <tr> put this..
<tr id="div_one">

meshi
09-06-2007, 04:33 PM
thank you che_anj..it really works



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum