Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

how to run php file inside a javascript code/json/jquery

Featured Replies

Hello Guys,

I have a registration form I want to submit to my database. I am not good in javascript for very good in php. I have a form like this;

<form action="" class="idealforms" method="post">

<div class="idealsteps-wrap">

<!-- Institution's Detail -->

<section class="idealsteps-step">
<div class="field">
<label class="main">Organization:</label>
<input name="organization" id="organization" placeholder="Eg. ABC Int'l Ltd, Institute of Information" type="text" maxlength="70"><br/>
<span class="error"></span>
</div>

<div class="field">
<label class="main">Username:</label>
<textarea name="ursername" cols="20" rows="5"></textarea>
<span class="error"></span>
</div>



<div class="field buttons">
<label class="main"> </label>
<button type="button" class="prev">« Prev</button>
<button type="submit" class="submit" name="submit_agm" id="submit_agm">Submit</button>
</div>
</form>

I have the php file which will insert the form into mysql

$organization = $_POST['organization'];
$org_address = $_POST[org_address];
$submit = mysqli_query($mysqli, "INSERT INTO meeting (page_name, organization, submitted_on) VALUES('$page', '$organization', CURRENT_TIMESTAMP)");

I have s javasctipt like this

<script>

$('form.idealforms').idealforms({

silentLoad: false,

rules: {

},

errors: {
'username': {
ajaxError: 'Username not available'
}
},

onSubmit: function(invalid, e) {
e.preventDefault();
$('#invalid')
.show()
.toggleClass('valid', ! invalid)
.text(invalid ? (invalid +' invalid fields') : 'Submitted Successfully!');

<?php includes('path_to_the_file.php'); ?>
}
});

$('form.idealforms').find('input, select, textarea').on('change keyup', function() {
$('#invalid').hide();
});

$('form.idealforms').idealforms('addRules', {
'comments': 'required minmax:50:200'
});
</script>

with the above php inside d javascript, if the content of path_to_the_file.php
$submit = mysql_query("INSERT INTO agm (page_name, organization, submitted_on) VALUES('Page', 'Organization', CURRENT_TIMESTAMP)");
it will be submitted to my database. but if it is
$submit = mysqli_query($syntaxit, "INSERT INTO agm (page_name, organization, submitted_on) VALUES('$page', '$organization', CURRENT_TIMESTAMP)");
it will not.

I also tried this method but didn't submitted anything at all to the database.

<script>

$('form.idealforms').idealforms({

silentLoad: false,

rules: {

},

errors: {
'username': {
ajaxError: 'Username not available'
}
},

onSubmit: function(invalid, e) {
e.preventDefault();
$('#invalid')
.show()
.toggleClass('valid', ! invalid)
.text(invalid ? (invalid +' invalid fields') : 'Submitted Successfully!');
}
});
</script>
<script>
$(function () {
$('form').bind('submit', function () {
$.ajax({
type: 'post',
url: 'sub.php',
data: $('form').serialize(),
success: function () {
alert('form was submitted');
}
});
return false;
});
});
</script>

Please which other way can i use to process my string php file in javascrpit/json/jquery and will work fine?

Thanks

mysql_query & mysqli_query are two similar but different functions. reading the W3 pages for each of the functions, there are some things you should notice
1. how you connect is different.
1a.) mysql_query needs to connect with a mysql_connenct command
1b.) mysqli_query needs to connect with a mysqli_connect command
2. what each function does and what they return are different

2a.) mysql_query performs a query on the data base and returns a handle of the query
2b.) mysqli_query performs a query against the data base(why different words on/against I don't know) and it will return -> For successful SELECT, SHOW, DESCRIBE, or EXPLAIN queries it will return a mysqli_result object. For other successful queries it will return TRUE. FALSE on failure

so you see they are different and can not be used interchangeably without some kind of function to clamp the data in way that each function could use.

 

for more information about the two functions you can read more at W3 on mysql_query and mysqli_query.

Also if you want to get a bigger grasp on the mysqli functions and how it all works together W3 has a reference page for that as well.

 

EDIT: corrected syntax and spelling errors and added some resource links.

Edited by drt_t1gg3r

Thanks drt_t1gg3r for giving you more knowledge.

I am a newby to web design and I do apologize. Sorry I didn't answer your question directly if at all really. I wish sql stuff was more apparent to me, however, sql techniques are a bit beyond my scope and I shouldn't have answered.

 

EDIT: corrected spelling errors

Edited by drt_t1gg3r

$submit = mysql_query("INSERT INTO agm (page_name, organization, submitted_on) VALUES('Page', 'Organization', CURRENT_TIMESTAMP)");

it will be submitted to my database. but if it is

$submit = mysqli_query($syntaxit, "INSERT INTO agm (page_name, organization, submitted_on) VALUES('$page', '$organization', CURRENT_TIMESTAMP)");

it will not.

 

I can't comment on the javascript as i'm 'getting there' with php. PHP 5 is mysqi and mysql is retiring. With the new mysqli it's more secure but it's a bit more long winded IMO. I assume somewhere in the php file you have a connection with username, db, password etc? Check prepared statements on the php site.

 

Anyway for the php you will need to connect, prepare the object or statement by binding the parameters and then executing the query. Also I see you are not escaping the string etc too, you need to cleanse the inputs before inserting into the db as this is a huge issue.

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.