Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 510
  • Last Modified:

Run a php script with a form select AJAX

I asked this question and now that I have the book I am trying to implement the code.

This was the accepted answer from Dave Baldwin.

I'm trying to call a php file that will echo html. I may need this spelled out a little more for me.

var myDiv = document.getElementById("myDiv");
document.getElementById("mySelect").onchange = someFunction(this);

function someFunction(object){  
    $.ajax({
        url: 'yourscript.php',
        data: 'content=' + object.value,
        cache: false,
        error: function(e){
            alert(e);
        },
        success: function(response){
            // A response to say if it's updated or not
            alert(response);
        }
    });  
}
0
burnedfaceless
Asked:
burnedfaceless
  • 19
  • 9
  • 3
2 Solutions
 
burnedfacelessAuthor Commented:
And I want it when the user selects one of the options.

I'm going to have a series of

if
else if
else if
else

so if they don't select anything the correct html will be output.
0
 
Marco GasiFreelancerCommented:
//This line is never used in the code you provided
var myDiv = document.getElementById("myDiv");

//when select called 'myselect' changes someFunction is called passing it the value of the select
document.getElementById("mySelect").onchange = someFunction(this);

//here the function: object is the variable name which holds the select content
function someFunction(object){  

//Ajax starts here
    $.ajax({

        //the script name
        url: 'yourscript.php',

         //the data passed to the script, that is the value of your select
        data: 'content=' + object.value,

        // always repeat the call
        cache: false,  

        // if error show an alert with error message
        error: function(e){
            alert(e);
        },

        // if success show an alert with the response, that is the html you expect
        success: function(response){
            // A response to say if it's updated or not
            alert(response);
        }
    });  
}

For the second part of the quesiton I need you are more descriptive, please :)
0
 
burnedfacelessAuthor Commented:
Visit the following URL: http://conutils.com/dist/rates/.

Then visit it from a mobile phone. The menu distorts is distorted any time a form is opened.


So I was going to have a series of if else statements in a php script that would echo the entire page, including the menu.

The final else would be if they clicked done, in which case the form would be rewritten and the menu not distorted.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
burnedfacelessAuthor Commented:
I'm assuming it would be on click rather than on change, right?
0
 
Marco GasiFreelancerCommented:
Sorry, I was away...
Okay, I understand. You have a page where user can use a select to view some details. If user makes a choice using the select you need to rewrite the form, but what are the other conditions? I ask this because I need to right understand what exactly you need to do in order to try to help you... and I still didn't clearly understand it.
0
 
Ray PaseurCommented:
Here is the "Hello World" showing how to use jQuery to make an AJAX request to a server-side script, and use the returned values to modify the DOM, changing the appearance of the web page.  Once you understand this, it should be easy to extend the concept to your particular HTML requirements.
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Jquery/A_10712-The-Hello-World-Exercise-with-jQuery-and-PHP.html
0
 
burnedfacelessAuthor Commented:
Well the desktop page works, if it ain't broke don't fix it.

I need to actually rewrite the entire page.

So from a mobile device when a person makes a selection a php script is called that echoes html of the entire page depending on the select option selected.

Does this help?
0
 
Marco GasiFreelancerCommented:
Sorry, but IMHO there is some wrong design pattern here. Ajax is useful because it allows to update only a portion of your page avoiding to refresh the whole page: if you use Ajax to refresh the whole page accordigly to the user choice you can simply leave Ajax and use a normal html form which posts data to a php scripts which re-echoes the whole page.

And if the problem is the different behavior depending on the user device, you should learn about responsive layout using html5, css3 and a bit of javascript letting php write contents.

I've the impression you are doing too mutch work in the wrong direction...
0
 
Ray PaseurCommented:
I agree with @marqusG (I usually do).  There are many strategies for success here, and they all start with getting the HTML right before you send it to the browser.  You may be able to detect the client browser and choose an alternate HTML library that fits the mobile device.  Or you may be able to create a responsive layout.  

You might find this easier with a clear understanding of the client / server relationship.  Basically, your PHP script will receive a request from the client.  At that time the logic that you build into the PHP script will prepare the HTML document and send it to the browser.  Then the server will disconnect and wait for the next request.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/A_11271-Understanding-Client-Server-Protocols-and-Web-Applications.html
0
 
Ray PaseurCommented:
Please see http://www.laprbass.com/RAY_temp_burnedfaceless.php

You might want to install this on your server and visit it from several different browsers and devices.  You can make a list of the desktop vs mobile user agents.  This might give you a good path toward creating the right HTML for the device the (human) client is using.

<?php // RAY_temp_burnedfaceless.php
error_reporting(E_ALL);


// DEMONSTRATE HOW TO FIND INFORMATION ABOUT THE CLIENT BROWSER
// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28311492.html


echo '<pre>';
var_dump($_SERVER['HTTP_USER_AGENT']);

Open in new window

HTH, ~Ray
0
 
burnedfacelessAuthor Commented:
The problem is all mobile devices I've tested have this problem.
0
 
Ray PaseurCommented:
Please gather your mobile device collection and make one test for each device using the script here:
http://www.laprbass.com/RAY_temp_burnedfaceless.php

Then show us the list of devices along with the output from that script.  It's unlikely that you will find a magic bullet.  It's more likely that you will find some kind of adapter pattern that you can put into the PHP script in order to choose the correct HTML for the device.
0
 
burnedfacelessAuthor Commented:
Thanks Ray I just got in from the office and have been unable to work on this.
0
 
Ray PaseurCommented:
NP, just show us what you can find when you have the time.  I look at EE once or twice a day and I'll get notified when you post the test results.  It's not any kind of magic, just plodding research and record keeping.  We will probably be able to discern patterns in the data that you gather and share with us.
0
 
burnedfacelessAuthor Commented:
I plan to do it tomorrow, actually.
0
 
burnedfacelessAuthor Commented:
To begin with here is my iPhone 5S. Let me get you some more extensive information. This will take a while.

string(138) "Mozilla/5.0 (iPhone; CPU iPhone OS 7_0_4 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11B554a Safari/9537.53"
0
 
burnedfacelessAuthor Commented:
Ray I don't want you to think I've forgotten. I had other work yesterday - our original domain was for sale and we purchased it. I have work to take care of and I plan to go to Verizon, Wal Mart, every store and enter the URL and note whether or not it works.

I also need to do some SEO work. I was told, granted by a go daddy salesman that you can't rely on google to generate all keywords.
0
 
Ray PaseurCommented:
I would be highly skeptical of anything a GoDaddy salesman told me, and I would be highly keen and alert to anything Google told me.  After all, Google is where people will be finding your site!  In my experience, Google AdWords is an amazing bargain, inexpensive, effective and accurate.  It's the whole reason the newspaper industry has collapsed in America -- Google is that much better than print advertising.
0
 
burnedfacelessAuthor Commented:
So meta keywords are unnecessary? - when google says they ignore them and when I go into google webmaster and see our keywords I should trust them?


The thing is is Consolidated Utilities, Inc. is an individual company but the other affiliate companies are lumped under Consolidated.

Ideally I want those companies to give us the top hit, as google already does for consolidated utilities.

Here's the linked files he sent me are they propaganda?

http://support.godaddy.com/help/article/6578?locale=en&ci=4606
http://support.godaddy.com/help/article/8499?locale=en&ci=4606
http://support.godaddy.com/help/article/5323?locale=en&ci=4606
http://support.godaddy.com/help/article/6541?locale=en&ci=4606
http://support.godaddy.com/help/article/6622?locale=en&ci=4606

If in your opinion it is unnecessary I will cancel auto renew.

Honestly man, we got the top hit after I added our site to google places and the other engines followed suit.

edit: I have to meet with the president today the phone testing will probably be tomorrow. I'm always working right now.
0
 
burnedfacelessAuthor Commented:
Ray - I just noticed all of our affiliate systems have a google place account. I'll just do what I did and we should be good.

My next post will be topic thread.

Thanks man I really appreciate it.

We are outsourcing our online bill pay because it allows customers to see usage and conserve water. But I hope to learn from y'all.

And I did change my minor to computer science.

Best regards,
Brian
0
 
Ray PaseurCommented:
always working right now
Good problem to have!

Meta keywords are necessary.  What does your company do?  If you make bicycles, your keywords would include "bicycle, bicycles."  Google is not the only search engine, just the predominant one.  I doubt that they ignore keywords, but there is a "keyword loading" concept that has been used in the past to exploit Google's search indexing algorithm.  I expect they have figured out how to remove the effect of keyword loading.

It all comes down to relevance from the perspective of the Google searcher.  As long as Google can create consistently relevant results, it has value and can sell advertising.  So expect that any "trick" designed to deceive Google or its users will be discovered and removed from the Google relevancy rankings.

I have found the following to work well:

1. The HTML HEAD <title> tag should contain a comma-separated list of highly relevant keywords.  Example:
<title>LAPRBASS | Lake Anna, Potomac River, Rappahannock River Bass Tournament Series</title>

2. HTML HEAD <description> tag is worth giving some time and attention to.  Example:
<META NAME="description" content="LAPRBASS runs competitive and lucrative professional bass fishing tournaments in Virginia. On the web at www.LAPRBass.com" />

3. The <h1> tag on the page should be relevant.

4. You should buy Google AdWords!  A few hundred dollars spent there is worth more than all the newspaper blow-ins put together.  The reason goes to relevancy.  In consumer product sales, a print advertising response rate of 1/2 of 1% is considered highly successful.  This is because you're advertising to anyone who bothers to read the ads.  Google only advertises to people who already are looking for your products or services.   Google AdWords response rates are many times more and cost much less.  So don't let the president pay for a listing in the Yellow Pages or something like that.  It just doesn't work any more -- the same money (or even much less money) spent on Google AdWords will get you far better results.
0
 
burnedfacelessAuthor Commented:
OK finally mobile testing today.

I have created a sub directory called test.

The page uses absolute positioning I think so it won't render right but if a form submission doesn't mess up the menu...I'll be back with data.
0
 
burnedfacelessAuthor Commented:
On Android I did not have the menu distortion problem here is the string I got.

String (137) "Mozilla/5.0 (Linux; android 4.1.2; VS890 4G Build/JZ054K) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.58 Mobile Safari /537.31"

There's more to debug but that's beyond this question.

Thanks Ray.
0
 
burnedfacelessAuthor Commented:
Ray if you need more data let me know. It's an iPhone problem. Even on chrome.

All droids worked.
0
 
burnedfacelessAuthor Commented:
For convenience on problem devices here is the mobile code my iPhone on safari
string(138) "Mozilla/5.0 (iPhone; CPU iPhone OS 7_0_4 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11B554a Safari/9537.53"

my iPhone on chrome

string(145) "Mozilla/5.0 (iPhone; CPU iPhone OS 7_0_4 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) CriOS/31.0.1650.18 Mobile/11B554a Safari/8536.25"

my iPhone on sky fire, which worked but still expanded the menu

string(121) "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_7; en-us) AppleWebKit/530.18 (KHTML, like Gecko) Version/4.0 Safari/528.17"
0
 
burnedfacelessAuthor Commented:
Ray I'm assuming you need data on as many devices that don't work.

I am going to get different iPhone versions. If you need info on devices that run correctly let me know.
0
 
burnedfacelessAuthor Commented:
Also what exactly are we attempting here?

Would we detect iPhone and say this page is not supported by iPhone then code a mobile version and work on iPhone?

I'm not sure how we are using PHP. Verizon is open until 8 so I will be back with lots of data for you.

Could you post an article on how we are using php so I can better understand what we are doing?

Thanks Ray
0
 
burnedfacelessAuthor Commented:
I'm pretty sure this problem is unique to iOS I haven't been able to test older iOS systems I have.

I posted a question on how to report a bug and this is the response. I interpreted this as it was forwarded to some part I have no access to...

forum
edit: not sure where to go from here
0
 
Ray PaseurCommented:
Sorry - we had a blackout from the storms so I've been away for a while.

iPhone:
string(138) "Mozilla/5.0 (iPhone; CPU iPhone OS 7_0_4 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11B554a Safari/9537.53"

string(138) "Mozilla/5.0 (iPhone; CPU iPhone OS 7_0_4 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11B554a Safari/9537.53"

string(145) "Mozilla/5.0 (iPhone; CPU iPhone OS 7_0_4 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) CriOS/31.0.1650.18 Mobile/11B554a Safari/8536.25"

"iPhone on sky fire, which worked but still expanded the menu" -- not sure what to make of that.

Android:
String (137) "Mozilla/5.0 (Linux; android 4.1.2; VS890 4G Build/JZ054K) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.58 Mobile Safari /537.31"

It appears that all or at least most instances of iPhone access cause the USER_AGENT to contain the string iPhone, so I would set up two versions of the HTML document.  One would be for iPhone, the other would be for all other browsers.  (Actually a sensible design would be an "adapter" that returns the location of the HTML document appropriate for the device.  Then when you find another device that has quirks like iPhone, you can just add a new element to the adapter).

Here is the PHP sample:

<?php // RAY_temp_burnedfaceless.php
error_reporting(E_ALL);

// THE SIGNAL STRING
$signal = 'iPhone';

// TEST FOR THE PRESENCE OF THE SIGNAL STRING
if (strpos($_SERVER['HTTP_USER_AGENT'], $signal) !== FALSE)
{
    echo 'Request is from iPhone';
}
else echo 'Request is not from iPhone';

Open in new window

Best regards, ~Ray
0
 
burnedfacelessAuthor Commented:
Thanks Ray. But how would the html differ for iPhone? Would it be in the Doctype?

Or would this be considered an iOS bug? It seems to be in the operating system.
0
 
Ray PaseurCommented:
I don't know how the HTML might differ for the iPhone.  I think the HTML5 doctype declaration would be the right thing to use.  You may have to experiment with the HTML to get it tailored for the iPhone.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 19
  • 9
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now