...

View Full Version : Passing string by post



Jenny Dithe
11-11-2010, 05:03 AM
Hi,

I have two values I wish to pass to a form using php with ajax, so I thought the simplest method would to be to create a string, post the string and then explode it on the form side to get the two values. However I am not entirely sure the correct syntax for posting a string.

(I get the theme results from one product, [this is what's below] -> then I create a set of buttons for each theme then when someone clicks on that theme I wish to call a table listing all the matches for that product that are in that theme.)

I am currently trying:


while($row = mysql_fetch_array($result)){
$tild=$row['product'] . ',' . $row['theme'];
echo '<input type="text" class="hidden" name="producttheme" id="$tild" value="$tild" ><input type="button" class="button3" name="theme" id="theme" value="' . $row['theme'] . '" onClick="loadXMLDoc2(\'getShow.php\',\'txtHint\',\'$tild=\'+encodeURI(document.getElementById(\'$tild\').valu e))" >&nbsp';
}


And then how would I retrieve it on getShow.php
by $_POST['$tild'] is clearly wrong.

MattF
11-11-2010, 10:00 AM
by $_POST['$tild'] is clearly wrong.

There is no $tild in your receiving script. There should be a tild, however.

Jenny Dithe
11-11-2010, 10:20 AM
Ok so if my value I am posting from my form is value="$tild"

I can retrieve it with

$_POST['tild']

is that what you are saying?

poyzn
11-11-2010, 10:23 AM
it should be $_POST['producttheme'] = $tild, but you're creating a bunch of input fields with the same name="producttheme",
by the way what's the point of assigning a value to variable $tild and then echoing it in single quotes?

MattF
11-11-2010, 10:48 AM
it should be $_POST['producttheme']

As pozyn said. I'd missed that being the input name. I'd only noted the example POST var. :D

Jenny Dithe
11-12-2010, 05:23 AM
I need to do the echo ' because I have:
onClick="loadXMLDoc2(\'getShow.php
etc

And I thought to backslash out commas to get them to work, it had to be within quotation marks within commas. Is there another way?

So based on what I understand you to be saying my code now reads


while($row = mysql_fetch_array($result)){
$tild=$row['product'] . ',' . $row['theme'];
echo '<input type="text" class="hidden" name="$tild" id="$tild" value="$tild" ><input type="button" class="button3" name="theme" id="theme" value="' . $row['theme'] . '" onClick="loadXMLDoc2(\'getShow.php\',\'txtHint\',\'$tild=\'+encodeURI(document.getElementById(\'$tild\').valu e))" >&nbsp';
}


Does this work? And if so I still don't know how I retrieve the value.

If it wasn't a variable I would normally use $_POST['tild'] but it is a variable so what would I do?

Thank you for your help so far I am really appreciating it.

poyzn
11-12-2010, 05:32 AM
please show loadXMLDoc2 function,
actually, you don't even need a hidden field, place your variable directly in button onclick function, try something like this:


while($row = mysql_fetch_array($result)) {
$tild = $row['product'] . ',' . $row['theme'];
echo '<input type="button" class="button3" id="theme" value="' . $row['theme'] . '" onClick="loadXMLDoc2(\'getShow.php\', \'txtHint\', \'encodeURI("' . $tild .'")\')" />&nbsp';
}

and then, in loadXMLDoc2 get it as third parameter

Jenny Dithe
11-12-2010, 07:34 AM
OK so what I have now is:



<script type="text/javascript">
function loadXMLDoc2(File,ID,Msg){
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
}
else {
try{
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}

xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById(ID).innerHTML=xmlhttp.responseText;
}
}

var params=Msg;
xmlhttp.open("POST",File,true);

xmlhttp.setRequestHeader("Pragma", "Cache-Control:no-cache");
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", params.length);
xmlhttp.setRequestHeader("Connection", "close");
xmlhttp.send(params);
}
</script>
</head>
<body>

//select from code

while($row = mysql_fetch_array($result)){
$tild=$_POST['product'] . ',' . $row['theme'];
echo '<input type="button" class="button3" name="theme" id="theme" value="' . $row['theme'] . '" onClick="loadXMLDoc2(\'getShow.php\' , \'txtHint\' , \'encodeURI($tild)\')" >&nbsp';
}


Using " leads to errors. And '.$tild.' leads to the error message that I am missing a )

However the getShow.php field seems to be blank, I have tried calling it by:
$below=$HTTP_GET_VARS["tild"];
echo $below;
echo $_POST['tild'];
echo "$tild 1";
echo $tild;

poyzn
11-12-2010, 08:02 AM
ok,
in separate php-file do next:


<?php

$row = array('product,1', 'product,2', 'product,3');

for($i=0; $i<count($row); $i++) {
echo '<input type="button" class="button3" id="theme" value="' . $i . '" onClick="loadXMLDoc2(\'getShow.php\' , \'txtHint\' , encodeURI(\'tild='. $row[$i] .'\'))" >&nbsp';
}

?><script type="text/javascript">
function loadXMLDoc2(File,ID,Msg){
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
}
else {
try{
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}

xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById(ID).innerHTML=xmlhttp.responseText;
}
}

var params = Msg;
xmlhttp.open("POST", File, true);

xmlhttp.setRequestHeader("Pragma", "Cache-Control:no-cache");
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", params.length);
xmlhttp.setRequestHeader("Connection", "close");
xmlhttp.send(params);
}
</script>
<div id="txtHint"></div>



and in getShow.php:



echo $_POST['tild'];


to see if it works

Jenny Dithe
11-12-2010, 09:28 AM
There is going to be only one product but several themes:
e.g. Skirt -> day wear, evening wear, work wear
or Trainers ->sports wear, weekend wear
so the user will first click on the skirt

then these buttons will be populated saying day, evening, work, but the number of themes will change by item selected [THIS IS WHERE THIS BIT IS]
then if the user clicks on the button saying "day" I want to send $row['product'] . ';' $row['theme']; OR skirt;day to my form

which will be sent to my getShow.php form so that I can explode the array into two variables, $item and $theme and select from my table where item='$item' and theme='$theme' thus calling a list of skirts that we have on offer which are suitable for day wear.

So the string I am sending should just have one product and one theme.

But there will be several buttons to click on, each with a different value. So the problem I had originally was naming them and giving them a distinct id so when they were clicked the correct value was sent to my form. Previously I had given them the same id and name but different value so the value of the first button would be sent to my form irrelevant of which button I clicked.

I hope this makes it a bit clearer.

poyzn
11-12-2010, 10:00 AM
ok, I've made it for you.
you can test it by creating a test.php file with only next content:


<?php

$row = array('Skirt' => array('day wear', 'evening wear', 'work wear'),
'Trainers' => array('sports wear', 'weekend wear'));

foreach($row as $theme => $item) {
echo $theme . ': ';
for($i=0; $i<count($item); $i++) {
echo '<input type="button" class="button3" id="theme" value="' . $item[$i] . '" onClick="loadXMLDoc2(\'getShow.php\' , \'txtHint\' , encodeURI(\'theme=' . $theme . '&item='. $item[$i] .'\'))" >&nbsp';
}
echo '<br />';
}
?><script type="text/javascript">
function loadXMLDoc2(File,ID,Msg){
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
}
else {
try{
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}

xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById(ID).innerHTML=xmlhttp.responseText;
}
}

var params = Msg;
xmlhttp.open("POST", File, true);

xmlhttp.setRequestHeader("Pragma", "Cache-Control:no-cache");
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", params.length);
xmlhttp.setRequestHeader("Connection", "close");
xmlhttp.send(params);
}
</script>
<div id="txtHint"></div>



and getShow.php:



<?php echo $_POST['theme'] . ' : ' .$_POST['item'];


all that is left to do is create $row array from your database.

Jenny Dithe
11-12-2010, 10:31 AM
That works great, thank you, sorry I was being a bit slow.

That works perfectly.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum