Jump to content

Can I pass Ajax var result to PHP code?


kristo5747

Recommended Posts

**Disclaimer: It's been a while since I last wrote any code. The quality of my code is likely to be sub-par. You've been warned.**

 

Greetings.

 

I am coding a basic form that uses a SELECT list I populate from my database.

 

However, my user needs the form to be dynamic and wants to be able to select either `MasterTable1` or `MasterTable2` or `MasterTable3`...

 

Instead of hardcoding the table name for the database query that populates the SELECT list, I attempted to implement a basic Ajax action (used example from w3schools)...and that's when I lost my sanity...

 

I can output `<div id='txtHint'></div>` in my page and it shows the correct table name that was picked.

 

But how do I pass the correct table name to my query that will populate my SELECT list???

 

I tried

 

     <select name="DBFilename" id="DBFilename" size="0"> 
     <option value="">Select Filename</option> 
    <?php 
    $sql="select distinct filename from "."<div id='txtHint'></div>";
    $result = mysql_query($sql);
     while ($row = mysql_fetch_array($result)){ ?>  
     <option value="<?php echo $row['filename']; ?>"><?php echo $row['filename']; ?></option> 
     <?php } ?> 
     </select> 

 

But to no avail. This is confusing  since I can do this...

 

    ...
        document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","gettable.php?q="+str,true);
    xmlhttp.send();
    }
    </script></head>
    <body><form><select name="SrcTbl" id="SrcTbl" size="0" onchange="showTable(this.value)"> 
    	<option value="">Select Data Table</option> 
    	<option value=""> </option> 
<option value="MasterTable1">up to 31 days old</option> 
<option value="MasterTable2">62 days old</option> 
    </select>
    </form><br /><div id="txtHint"><select name="tabList"><option></option></select> </div>
    </body></html>

 

And the name of my table will be displayed in the SELECT list 'tablist'.

 

That <div> supposed to end up being a table name that is returned by the Ajax script. How do I pass the correct table name to my query that will populate my SELECT list? Thanks!!

 

Form code in pastebin.com

Link to comment
https://forums.phpfreaks.com/topic/246313-can-i-pass-ajax-var-result-to-php-code/
Share on other sites

Can I pass Ajax var result to PHP code?

Yes! That's exacly what AJAX is for.

 

In the second code group you published you have this line

xmlhttp.open("GET","gettable.php?q="+str,true);

 

Your running the gettable.php script and passing str to it. The str is where you tell the PHP script (and it has to be a php script) to select either `MasterTable1` or `MasterTable2` or `MasterTable3`; ie, var str = 'MasterTable2';

 

Then your php script gets that info:

IF (isset($_GET['q'])){

opens and connects to your database  <<<<=== of course this is not php but english explanation of it

$thing = $_GET['q'];

then query => SELECT * FROM $thing;

make your <select><option> thing here and echo it

}

 

or better use heredoc.

 

Put this in your php script and see what is sent back down and printed in your <div id='txtHint'></div>

 

echo <<<EOD
<table width="300px"><tr>
	<td align="left">
		<input type="button" name="read_message" value="Read" onclick= "message_read()" />
	</td>
	<td align="center">
			<input type="button" name="action_message" value="Delete" onclick= "message_delete()" />
	</td>
	<td align="right">
		<input type="button" name="reply_message" value="Reply" onclick="message_reply()" />
	</td>
	</tr>
</table>
EOD;

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.