LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-16-2024, 03:14 PM   #1
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Rep: Reputation: 12
Ajax call not processing correctly


I'm using Ubuntu 22.04

My code allows selection of vendors from a database except when; "if (choice=='Add Vendor to List')". When that happens I want to enable the $.ajax call to be able to add a new vendor to the database and re-do the original code again where the added vendor will now be in the database and available as a choice in the selection.

Thanks in advance in solving this.

R

Code:
<script type="text/javascript" >  // Drop Down List for Vendors
var choice="";
var myValues=[];
var vendorValues=[];
var fieldsKeys=[];
var fieldsVals=[];
var addFlag=false;

$(document).ready(function() {
		
$("#rbox").hide();	

//var vendorinput="input[name='Vendor']"  // gets vendors from database
	
$("input[name='Vendor']").focus(function(){
	
	$("#rbox").show();
		
		var request=$.ajax({
		url: 'vendorList.php',
		dataType: "json"
		})

	request.done(function(retdata){  
		
	vendorValues=Object.values(retdata)  // create array of values from retdata json string object
		//$('#rbox').append($("<h4>Vendors&nbspAvailable&nbsp;to&nbsp;Pick</h4>"))	used caption see line 224
     $('#rbox') .append("<select id='selbox' name='selbox' size='10'></select>");

 for (var i=0; i<retdata.length; i++) {
	myValues[i]=vendorValues[i]['Vid']+". "+vendorValues[i]['Vendor']; // array of values for each retval object 
}               
          
      $('#selbox').append("<option selected disabled>&nbsp;---------------------------------&nbsp;Select Vendor&nbsp;------------------------------</option>"); // Add Caption 
      $('#selbox').append("<option>Add Vendor to List</option>");                
      for (const val of myValues) {
      	$('#selbox').append($("<option>").prop({value: val, text: val}));
      	$('#rbox').append("<br><br>");	
      }
     //$('#selbox').append("<option value='2'>Update Vendor List</option>");       

$("#selbox").on('change', function () {
	choice=$("#selbox option:selected").text();
	
	if (choice=='Add Vendor to List') {
		addFlag=true;
	}

	$("input[name='Vendor']").val(choice);  // 'Vendor' value is populated w/ choice

})	// selbox
}) // done

console.log("line 324 "+choice+addFlag);	

if (addFlag=true) {
	
console.log('line 328 addFlag is true');

		var request2=$.ajax({
		url: 'addVendor.php',          //Gets to 'addVendor' but does not run it
		dataType: "text"
		})

	request2.done(function(retdata){ 
		alert("line 336 "+retdata);     //repeats showing of all code each time you press enter key see Attachement.
})

} // if addFlag

}); // input


}) // ready

</script>
addVender.php is:
Code:
<!DOCTYPE html>
<html>

<head>
</head>

<body>
<h2>This is a test program for insertQuery.php </h2>
	<?php
		// Defining variables
		$name = $email = $level = $review = "";

		// Checking for a POST request
		if ($_SERVER["REQUEST_METHOD"] == "POST") {
		$name = test_input($_POST["name"]);
		$email = test_input($_POST["email"]);
		$review = test_input($_POST["review"]);
		$level = test_input($_POST["level"]);
		}

		// Removing the redundant HTML characters if any exist.
		function test_input($data) {
		$data = trim($data);
		$data = stripslashes($data);
		$data = htmlspecialchars($data);
		return $data;
		}
		?>

		<h2>PHP Form Example: GFG Review</h2>
		<form method="post" action=""
			//"<?php echo htmlspecialchars($_SERVER[" PHP_SELF "]);?>">
			Name:
			<input type="text" name="name">
			<br>
			<br> 
			E-mail:
			<input type="text" name="email">
			<br>
			<br>
			Review For GFG:
			<textarea name="review" 
					rows="5" cols="40">
			</textarea>
			<br>
			<br> 
			Satisfaction Level:
			<input type="radio" name="level" 
				value="Bad">Bad
			<input type="radio" name="level"
				value="Average">Average
			<input type="radio" name="level"
				value="Good">Good
			<br>
			<br>
			<input type="submit" name="submit"
				value="Submit">
		</form>

		<?php
			echo "<h2>Your Input:</h2>".$name."<br>";
			/*echo $name;
			echo "<br>";
			echo $email;
			echo "<br>";
			echo $review;
			echo "<br>";
			echo $level;*/
		?>

	
</body>
</html>
Attached Thumbnails
Click image for larger version

Name:	Selection_010.png
Views:	15
Size:	100.8 KB
ID:	42428  

Last edited by pizzipie; 01-17-2024 at 03:40 PM.
 
Old 01-17-2024, 08:02 PM   #2
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,662
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Arrange for wire-transfer of $100 an hour and I will then try to debug your code for you.
 
Old 01-17-2024, 11:07 PM   #3
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Original Poster
Rep: Reputation: 12
no thank you
 
Old 01-21-2024, 08:38 PM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,662
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
All right, let me kindly try again.

You said: "When [(choice=='Add Vendor to List')"] happens" I want to enable the $.ajax call to be able to add a new vendor to the database and re-do the original code again where the added vendor will now be in the database and available as a choice in the selection."

The "'Add Vendor to List'" represents a new path in the overall logic of your program. This is: "an exception." You must now re-direct the user to an entirely-new section of the software – "an entirely-new URL" – which will enable him to successfully add the new vendor.

Eventually, if he actually does succeed in adding a new vendor, your program logic must then re-direct him back to "where you are now." This time, the vendor will be there.

The flaw in your original statement of the problem is that "the HTTP protocol is stateless." You cannot directly express logic in the manner that you originally stated.

Last edited by sundialsvcs; 01-21-2024 at 08:44 PM.
 
1 members found this post helpful.
Old 01-21-2024, 08:56 PM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082
Quote:
Originally Posted by sundialsvcs View Post
You must now re-direct the user to an entirely-new section of the software – "an entirely-new URL" – which will enable him to successfully add the new vendor.
Do you know what AJAX is?

Quote:
The webpage can be modified by JavaScript to dynamically display—and allow the user to interact with the new information.
 
Old 02-15-2024, 12:49 PM   #6
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Original Poster
Rep: Reputation: 12
Thank You for your help. I figured this out finally and split to a new .php file.

R
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP/AJAX - Any way to Upload or transfer files from Ajax to folder pizzipie Programming 4 07-19-2023 08:55 AM
PHP - in-data from AJAX call will not work in SYSTEM() - using bash same data works pizzipie Programming 6 06-28-2014 12:30 AM
LXer: Solid Ajax applications, Part 2: Building Ajax back ends LXer Syndicated Linux News 0 01-22-2008 12:30 PM
LXer: Eclipse Ajax Toolkit Framework and Ajax tools LXer Syndicated Linux News 0 05-12-2006 12:21 PM
LXer: Mastering Ajax, Part 3: Advanced requests and responses in Ajax LXer Syndicated Linux News 0 02-16-2006 06:46 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration