Eksepshon Eksepshon - 2 years ago 63
PHP Question

Javascript: function that does one thing if php variable is define

I have an SQL table that is updated through PHP. On an editJob page, you can navigate to it and enter a job number in a text box that then inserts that job number into the SQL and creates an entry. You can then add operations to the job number.
However, what I want to be able to do is navigate to the page with PHP variables entered into the URL which then autofill the text box with the job number from the URL.
I can't figure out how to check whether this PHP variable is present in my javascript.

Here is the relevant javascript below:

<script type="text/javascript">
function jobCheckCallback (data) {

if (data.includes("<td")) {
alert("Job already exists. Edit this job by clicking Edit on the job's overview page");
document.getElementById("jobID").value = 0;
} else {
var njobID = document.getElementById("jobID").value;
if (njobID == "") njobID = 0;
else njobID= <?php echo $jobID; ?>;
sendAsync("editDatabase.php?sql=UPDATE+customerlist+SET+jobID="+njobID+" WHERE+jobID=" +jobID);
sendAsync("editDatabase.php?sql=UPDATE+operations+SET+jobID="+njobID+" WHERE+jobID="+jobID);
sendAsync("editDatabase.php?sql=UPDATE+jobfiles+SET+jobID="+njobID+" WHERE+jobID="+jobID);
sendAsync("editDatabase.php?sql=UPDATE+pallets+SET+jobID="+njobID+" WHERE+jobID="+jobID);
sendAsync("editDatabase.php?sql=UPDATE+jobs+SET+jobID="+njobID+" WHERE+jobID="+jobID,function(id){
return function(){

The error message I get is "SyntaxError: missing ; before statement" but I'm guessing its another issue causing this error.

EDIT: $jobID is the PHP variable that can be entered into the URL, and is then autofilled into the textbox on the page.

Answer Source

Have you checked the resulting page source? I suspect you need to wrap the php output statement with quotes so:

else njobID= '<?php echo $jobID; ?>';

So if job ID is empty it won't result in invalid js.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download