Jump to content

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>'; 
}
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.