Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    Regular Coder
    Join Date
    Sep 2010
    Posts
    460
    Thanks
    214
    Thanked 1 Time in 1 Post

    Passing string by post

    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:
    Code:
    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\').value))" >&nbsp';
    		}
    And then how would I retrieve it on getShow.php
    by $_POST['$tild'] is clearly wrong.

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Code:
    by $_POST['$tild'] is clearly wrong.
    There is no $tild in your receiving script. There should be a tild, however.

  • #3
    Regular Coder
    Join Date
    Sep 2010
    Posts
    460
    Thanks
    214
    Thanked 1 Time in 1 Post
    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?

  • #4
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    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?
    Last edited by poyzn; 11-11-2010 at 10:32 AM.

  • #5
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by poyzn View Post
    it should be $_POST['producttheme']
    As pozyn said. I'd missed that being the input name. I'd only noted the example POST var.

  • #6
    Regular Coder
    Join Date
    Sep 2010
    Posts
    460
    Thanks
    214
    Thanked 1 Time in 1 Post
    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
    Code:
    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\').value))" >&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.

  • #7
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    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:
    PHP Code:
        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
    Last edited by poyzn; 11-12-2010 at 05:42 AM.

  • #8
    Regular Coder
    Join Date
    Sep 2010
    Posts
    460
    Thanks
    214
    Thanked 1 Time in 1 Post
    OK so what I have now is:
    Code:
    <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;

  • #9
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    ok,
    in separate php-file do next:
    PHP Code:
    <?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:

    PHP Code:
    echo $_POST['tild']; 
    to see if it works
    Last edited by poyzn; 11-12-2010 at 08:08 AM.

  • #10
    Regular Coder
    Join Date
    Sep 2010
    Posts
    460
    Thanks
    214
    Thanked 1 Time in 1 Post
    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.

  • #11
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    ok, I've made it for you.
    you can test it by creating a test.php file with only next content:
    PHP Code:
    <?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 Code:
    <?php echo $_POST['theme'] . ' : ' .$_POST['item'];
    all that is left to do is create $row array from your database.
    Last edited by poyzn; 11-12-2010 at 10:33 AM.

  • Users who have thanked poyzn for this post:

    Jenny Dithe (11-12-2010)

  • #12
    Regular Coder
    Join Date
    Sep 2010
    Posts
    460
    Thanks
    214
    Thanked 1 Time in 1 Post
    That works great, thank you, sorry I was being a bit slow.

    That works perfectly.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •