Forum Moderators: coopster

Message Too Old, No Replies

redirect external php file output to calling page

         

snehula

11:44 am on May 19, 2011 (gmt 0)

10+ Year Member



Here I go again!
So, me being a complete idiot, I finally managed to check my database for info entered in a textbox, the code for this goes something like:


if(isset($_POST['findcust']) && !empty($_POST['findcust'])) 
{
$surname = $_POST["findcust"];
$sql = "SELECT * FROM customer WHERE lastname LIKE '".$surname. "'";
$result=mysql_query($sql);
$db_field = mysql_fetch_assoc($result);
foreach($db_field as $key => $value) {echo "Key is: ".$key. " <i>Value is:".$value. "</i><br/>";}


this is my way of trying to validate if the user actually entered anything in the textbox. I'm not sure if the isset method returns true even if there's nothing in the textbox, and if I need to check it for being empty as well, but I did just to make sure.

Now, this is working and printing what I need. Without using some ajax stuff, I guess I cannot redirect output to the calling page without refreshing it, so to get to the same page should i use header("Location: xx.php")? and if I want the textbox values to reapper on refresh, do i use something like:
echo "<script type=\"text/javascript\">document.getElementById('findcust').value = ".$_POST[findcust]'. ";</script>";"
?

Anyway, if let's say I refresh the page, I want to add the data to it in a nice table, so i tried the following, which is probably plain stupid thing to do, so I'd love to hear from you webmaster gurus how to do this :-))

$count=mysql_num_rows($result); 
if ($count == 0)
{
echo "<script type=\"text/javascript\"> alert('No record was found with name ". $surname . "');</script>";
}
else if($count)
{
$content = "<table id=\"resultset\">";
while ($db_field = mysql_fetch_assoc($result))
{
$content .= "<tr>";
$content .= "<td>".$db_field["firstname"]."</td>";
$content .= "<td>".$db_field["lastname"]."</td>";
$content .= "<td>".$db_field['address1]."</td>";
$content .= "<td>".$db_field['address2']."</td>";
$content .= "<td>".$db_field['county']."</td>";
$content .= "<td>".$db_field['home_phone']."</td>";
}
$content .= "</table>";
echo "<script type=\"text/javascript\">document.getElementById('servcont').innerHTML = ".$content. ";</script>";
}


where i am trying to put the whole data with formatting in a variable as a string and then add it onto a div on the page that i have for this purpose. This is obviously far from being right coz it's not working, so if someone could pleeease give me an idea.. :-)

rocknbil

6:29 pm on May 20, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, it's hard to tell if "it's not working" via problems in PHP or in Javascript. Share details and errors.

It **looks like** Javascript because normally you do

document.getElementById('servcont').innerHTML='Some content';

And you're doing

document.getElementById('servcont').innerHTML=Some content;

See the difference? You don't have quotes in the value you're setting which will kick a JS error. This is further complicated by quote marks, if your text has a quote anywhere, even my example fails

document.getElementById('servcont').innerHTML='I don't think this will work';

because it needs to be escaped

document.getElementById('servcont').innerHTML='I don\'t think this will work';

So you have another operation you need to perform, escaping the quotes, which brings me to . . . . why do you need to use Javascript at all?

You know this all fails if JS is disabled, right? Why can't you just output this chunk on the div you want to output it on?

<div id="servcont">
<?php
// your output code here
?>
</div>

Or better yet, do all your code at the top, and just store it in a variable:

<?php
// do all your stuff and you store output in $content
?>
<html>
<head>
<!-- etc -->
</head>
<body>
</body>
<div id="servcont">
<?php echo $content; ?>
</div>
</html>

The last bit, minor, you can optimize your code a little (this is just a FYI and will show no improvement in performance . . .


<div id="servcont">
<?php
// As mentioned, the code operations don't have to take place in the div,
// and can be at the top, but just for example . . .
$count=mysql_num_rows($result);
if ($count == 0) {
echo "<p>No record was found with name $surname</p>";
}
// If it's not 0, it must be greater, right?
else {
$content = null; // To squelch concatenation warnings
while ($db_field = mysql_fetch_assoc($result)) {
$content .= "<tr>
<td>".$db_field["firstname"]."</td>
<td>".$db_field["lastname"]."</td>
<td>".$db_field['address1]."</td>
<td>".$db_field['address2']."</td>
<td>".$db_field['county']."</td>
<td>".$db_field['home_phone']."</td>
</tr>"; // you missed closing tr
}
echo "<table id=\"resultset\">$content</table>";
}
?>
</div>

snehula

11:23 am on May 24, 2011 (gmt 0)

10+ Year Member



thanks, i removed the javascript and did what u suggested.. lovely :-)