1

*updated

The problem is there a multiple variables and i can't pass the php echo. and i can't just run the script within the php due to the " conflict

<?php
.....other stuff

 $i=0;
 while ($i < $num) {

$id=mysql_result($result,$i, "StudentID");
$first=mysql_result($result,$i,"First");
$last=mysql_result($result,$i,"Last");
$date= date("Y.m.d");
echo "<div id=\"list\"><div id=\"id\">$id</div><div id=\"name\"> $first $last  
<button onclick=\"addHit($id,$first,$last,$classname,$day,$times,$date)\">Checkin</button></div></div>  ";

$i++;
}

echo"</div></div></div></div>";

?>
<script>
function addHit(id, first, last, classname, day, times, date)
{
$.ajax({
   type: "POST",
   url: "checkinuser.php",
   data: "id=id&first=first&last=last&classname=classname&day=day&times=times&date=date",
   success: function(msg){
     alert( "Data Saved: " + msg ); //Anything you want
   }
 });
}
</script>
0

6 Answers 6

3

As others have mentioned, you can't have PHP directly in the HTML, so you need to escape all those variables in a PHP code block. If this were my code though, I'd consider refactoring a bit, so that I could use JSON encoded parameters, e.g.:

<?php
    $rawData = array('id'=>$id, 'first'=>$first, 'last'=>$last, ... );
    $hitData = json_encode($rawData);
?>

<button onclick="addHit(<?php echo $hitData ?>)">Checkin</button>

function addHit(inputData)
{
    $.ajax({
        type: "POST",
        url: "checkinuser.php",
        data: inputData
        success: function(msg) {
            alert( "Checked In! " + msg ); 
        }
    });
}

In response to your edit, if you want to keep the params the way you have them, I'd modify your while loop:

<?php
$i=0;
while ($i < $num) {

    $id=mysql_result($result,$i, "StudentID");
    $first=mysql_result($result,$i,"First");
    $last=mysql_result($result,$i,"Last");
    $date= date("Y.m.d");
?>
<div id='list'>
    <div id='id'>
        <?php echo $id?>
    </div>
    <div id='name'>
        <?php echo "$first $last" ?>
        <button onclick='addHit( <?php echo "$id,$first,$last,$classname,$day,$times,$date" ?> )'
            Checkin
        </button>
    </div>
</div>

<?php 
    $i++;
}
?>

The goal here is to make it easier to read/write, so separating static code (e.g. HTML) from dynamic code as much as possible can be a good thing. One big advantage is that you don't have to worry about escaping quotes like you do, though a disadvantage is having a fair number of <?php> blocks.

Also note that you're setting id='id' for all these divs, which won't work since they'll all share one id when id's need to be unique on a given page. You probably want to echo $id there too.

Similarly, there's no reason to have echo"</div></div></div></div>"; - move that outside the PHP block and just leave it as HTML.

Finally, in your javascript, you're data parameter is the literal string:

"id=id&first=first&last=last&classname=classname&day=day&times=times&date=date"

This won't work as id will literally be 'id', first will be 'first', etc.

You need to either use the method I have above (where you pass the whole parameter), or build up the object to pass as your data param. .ajax() will take objects, so I'd make it an object, and pass that, e.g.:

function addHit(id, first, last, classname, day, times, date)
{
inputData = { id: id, first: first, etc, etc}
$.ajax({
   type: "POST",
   url: "checkinuser.php",
   data: inputData,
   success: function(msg){
     alert( "Data Saved: " + msg ); //Anything you want
   }
 });
}
Sign up to request clarification or add additional context in comments.

6 Comments

awesome that looks a lot cleaner (sorry im still learning) what's the correct way to do the data in the java part
Modified to include the javascript (note that java and javascript are not interchangeable terms). Hope that helps!
the only problem now is i'm getting an unexpected identifier. not sure which one is causing that
Check the error line, and if necessary, start echoing things one at a time to narrow it down, e.g. echo $id; echo $first; echo $last;, etc . . . one of them will eventually be the one that's causing you grief; if you put each echo on it's own line, when it errors, you'll know exactly which variable is undefined
working now had to add '' for each variable wihtin the echo. thank you so much for all the help you are amazing!
|
2

Shouldn't the data look alike:

data: "id="+id+"&first="+first+"&last="+last+"&classname="+classname+"&day="+day+"&times="+times+"&date="+date,

You can also use the following format:

data : { id : id, first : first, last : last, classname : classname, day : day, times : times, date : date }

plus what mimiz said.

Comments

0

You can't insert PHP variables into other code directly. Try echoing them.

<button onclick=\"addHit('<?php echo $id.","$first.",".$last.",",$classname.",".$day.",".$times.",".$date; ?>')\">Checkin</button>

1 Comment

how would i do that if its in like $i=0; while ($i < $num) { $id=mysql_result($result,$i, "StudentID"); $first=mysql_result($result,$i,"First"); $last=mysql_result($result,$i,"Last"); $date= date("Y.m.d"); echo "<div id=\"list\"><div id=\"id\">$id</div><div id=\"name\"> $first $last <button onclick=\"addHit('$id,$first,$last,$classname,$day,$times,$date')\">Checkin</button></div></div> "; $i++; }
0

try this :

<button onclick=\"addHit(<?php echo " '$id', '$first' ,'$last','$classname','$day', '$times','$date'"; ?>)\">Checkin</button>

Regards;

mimiz

Comments

0

Try changing this:

type: "POST",

for this:

type: "GET",

You should pass the variables and receive them in your PHP code by GET, since it is passing by url.

Comments

-1

addHit('$id,$first,$last,$classname,$day,$times,$date') must be addHit($id,$first,$last,$classname,$day,$times,$date)

(tnx to Musa )

data: "id=id&first=first&last=last&classname=classname&day=day&times=times&date=date" must be data: "id=$id&first=$first&last=$last&classname=$classname&day=$day&times=$times&date=$date"

1 Comment

The URL parameter string can't have PHP in it, as it's part of the javascript code; if he wants to keep it the way it is, he'd have to build the string using the parameters he passed to the JS function, e.g. 'id=' + id + '&first=' + first, etc etc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.