Solved

Run a php script with a form select AJAX

Posted on 2013-12-05
31
496 Views
Last Modified: 2013-12-10
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
Comment
Question by:burnedfaceless
  • 19
  • 9
  • 3
31 Comments
 

Author Comment

by:burnedfaceless
ID: 39699126
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
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 39699173
//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
 

Author Comment

by:burnedfaceless
ID: 39699194
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
 

Author Comment

by:burnedfaceless
ID: 39699215
I'm assuming it would be on click rather than on change, right?
0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 39699473
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39699555
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
 

Author Comment

by:burnedfaceless
ID: 39699614
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
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 39699631
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39699655
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39699686
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
 

Author Comment

by:burnedfaceless
ID: 39700065
The problem is all mobile devices I've tested have this problem.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39701516
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
 

Author Comment

by:burnedfaceless
ID: 39702324
Thanks Ray I just got in from the office and have been unable to work on this.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39702555
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
 

Author Comment

by:burnedfaceless
ID: 39702559
I plan to do it tomorrow, actually.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:burnedfaceless
ID: 39703228
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
 

Author Comment

by:burnedfaceless
ID: 39704233
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39704382
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
 

Author Comment

by:burnedfaceless
ID: 39704446
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
 

Author Comment

by:burnedfaceless
ID: 39704538
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39704587
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
 

Author Comment

by:burnedfaceless
ID: 39706544
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
 

Author Comment

by:burnedfaceless
ID: 39707063
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
 

Author Comment

by:burnedfaceless
ID: 39707064
Ray if you need more data let me know. It's an iPhone problem. Even on chrome.

All droids worked.
0
 

Author Comment

by:burnedfaceless
ID: 39707090
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
 

Author Comment

by:burnedfaceless
ID: 39707151
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
 

Author Comment

by:burnedfaceless
ID: 39707204
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
 

Author Comment

by:burnedfaceless
ID: 39707470
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
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 500 total points
ID: 39708876
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
 

Author Comment

by:burnedfaceless
ID: 39708955
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
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39708974
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now