Jump to content

PHP echo problem with no error


cfgcjm

Recommended Posts

Hi everyone, I'm trying to run this script (1st below) and it's running errorless but not populating the fields as it's supposed too but if i switch it around (2nd below) below it does work. It must be my syntax but i'm not sure what.

 

How I want it to work and it doesn't now

<?php
session_start();
//Call dbconnection powerscript
require_once "connect.php";

//Pull vars from get url
$lastname = urldecode($_GET['param']);
$firstname = urldecode($_GET['param2']);

$sql = "SELECT * FROM records WHERE fname='$firstname' AND lname='$lastname'";
if ($r = mysql_query ($sql)) 
{
$row = mysql_fetch_array ($r);
$num = mysql_num_rows ($r);
if ($num > 0)
//If the Student Has an existing Record
{
$fname=$row['fname'];
$lname=$row['lname'];
$age = $row['age'];


//echo back script
echo '<script type="text/javascript">
document.getElementById("fname").value = $fname;
document.getElementById("lname").value = $lname;
document.getElementById("age").value = $age;
</script>'; 

}
}
?>

 

 

 

How it does work

<?php
session_start();
//Call dbconnection powerscript
require_once "connect.php";

//Pull vars from get url
$lastname = urldecode($_GET['param']);
$firstname = urldecode($_GET['param2']);

$sql = "SELECT * FROM records WHERE fname='$firstname' AND lname='$lastname'";
if ($r = mysql_query ($sql)) 
{
$row = mysql_fetch_array ($r);
$num = mysql_num_rows ($r);
if ($num > 0)
//If the Student Has an existing Record
{
$fname=$row['fname'];
$lname=$row['lname'];
$age = $row['age'];


//echo back script
echo '<script type="text/javascript">
document.getElementById("fname").value = "Christopher";
document.getElementById("lname").value = "Miller";
document.getElementById("age").value = "17";
</script>'; 

}
}


?>

Link to comment
https://forums.phpfreaks.com/topic/112013-php-echo-problem-with-no-error/
Share on other sites

Why are you dumping it out in a JavaScript tag? There are no actions in the JavaScript to call it. You can simply put those values right in the form elements if that is what you're trying to do.

 

Other things you're doing wrong:

 

1) You're pulling the data out before you check to see if you have any results. That's backwards.

2) Replace:

$fname=$row['fname'];
$lname=$row['lname'];
$age = $row['age'];

with:

extract($r);

 

Variables enclosed in single quotes are not evaluated.

Try this:

<?php
//echo back script
        echo '<script type="text/javascript">
        document.getElementById("fname").value = ' . $fname . ';
        document.getElementById("lname").value = ' . $lname . ';
        document.getElementById("age").value = ' . $age .;
        </script>'; 
?>

 

Ken

I added the error reporting and changed the echo script as ken prescribed and i'm still not getting the values in the fields and still no error. Below is my current script

 

<?php
session_start();
//Call dbconnection powerscript
error_reporting("E_ALL");
require_once "connect.php";

//Pull vars from get url
$lastname = urldecode($_GET['param']);
$firstname = urldecode($_GET['param2']);

$sql = "SELECT * FROM records WHERE fname='$firstname' AND lname='$lastname'";
if ($r = mysql_query ($sql)) 
{
$row = mysql_fetch_array ($r);
$num = mysql_num_rows ($r);
if ($num > 0)
//If the Student Has an existing Record
{
$fname=$row['fname'];
$lname=$row['lname'];
$age = $row['age'];


//echo back script
        echo '<script type="text/javascript">
        document.getElementById("fname").value = ' . $fname . ';
        document.getElementById("lname").value = ' . $lname . ';
        document.getElementById("age").value = ' . $age .';
        </script>'; 
}
}
?>

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.